我试图在react-native中成功发出post请求后发回一个响应,但是响应没有正确到达。
router.route("/addUser").post((req, res) => {
let name= req.body.name;
connection.query(
`INSERT INTO users (id,name) VALUES (NULL,${name})`,
function (error, results) {
console.log("REAL RESULTS: " + JSON.stringify(results));
if (error) throw error;
res.send("Inserted successfully");
}
);
});
然后,当我尝试接收请求并打印其内容(预期是:插入成功)时,我得到:
{“type”:“default”,“status”:200,“ok”:true,“headers”:{“map”:{“x-powered-by”:“express”,“content-length”:“21”,“connection”:“keep-alive”,“content-type”:“text/HTML;charset=utf-8”,“etag”:“w/”15-faifznmhvIIily1heybcwglwsmo“”,“date”:“Wed,12日Aug20 2020年12:35:59 GMT”,“cache-control”:
这是我发出请求的代码:
fetch("http://192.168.1.9:3000/database/addUser", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "Some name",
}),
})
.then((res) => {
console.log(JSON.stringify(res));
试试看
fetch("http://192.168.1.9:3000/database/addUser", {
method: "POST",
mode: "no-cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "Some name",
}),
})
.then(response => response.json())
.then(data => console.log(data));