我尝试在react-native中成功发布请求后发送回响应,但是响应未正确到达。
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”:“默认”,“ status”:200,“ ok”:true,“ headers”:{“ map”:{“ x-powered by”:“ Express”,“内容长度“:” 21“,”连接“:”保持活动“,”内容类型“:”文本/ html; charset = utf-8“,” etag“:” W /“ 15-fAIFznmhviiIlY1HeyBcwGlWsmo”“,”日期“:“格林尼治标准时间2020年8月12日星期三”,“缓存控制”:“公共,最大年龄= 0”}},“网址”:“ http://192.168.1.9:3000/database / addUser“,” bodyUsed“:false,” _ bodyInit“:{” _ data“:{” size“:21,” offset“:0,” blobId“:” aeb788fe-9bab-4641-a0a3-25e5df4c8890“,” __ collector “:{}}},” _ bodyBlob“:{” _ data“:{” size“:21,” offset“:0,” blobId“:” aeb788fe-9bab-4641-a0a3-25e5df4c8890“,” __ collector“:{ }}}}
这是我发出请求的代码:
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));
答案 0 :(得分:0)
尝试
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));