如何解决发出发布请求后,React-native无法响应
我尝试在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));
解决方法
尝试
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));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。