我已经做了一个基本的节点/表达服务器,并且有一个处理表单数据提交的路由(我已经使用react做出了),使用async / await和fetch api处理了发布请求。不知道问题出在我的服务器端路由还是我使用async / await提取的发布请求的实现。但是服务器确实收到了表单数据,只是没有返回响应。
我的代码:
节点/表达路线
router.post('/add', function (req, res) {
console.log(req.body);
res.json({success : "Updated Successfully", status : 200});
});
注意:console.log(打印预期数据,但客户端未正确接收响应)
请求后实现:
const postRequestHelper = async (routePath, objectPayload) => {
console.log("posting payload object: ");
console.log(objectPayload);
const rawResponse = await fetch(routePath, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(objectPayload)
});
const response = await rawResponse.json();
return response;
};
export default postRequestHelper;
提交提交请求的表单提交代码:
async handleSubmit(event) {
if(typeof this.state.validationMessages === "undefined"){
// create payload data object
let objectPayload = Object.assign({}, this.state);
for(let key in objectPayload){
if(!isInObject(key, formKeyConstants)) // delete any prop keys that aren't in formPropertyKeys js file
delete objectPayload[key]
}
// send post request
console.log(objectPayload);
const response = await postRequestHelper("http://localhost:8080/user/add", objectPayload);
// log response data
console.log("response");
console.log(response);
}
event.preventDefault();
}
答案 0 :(得分:1)
在服务器中尝试如何?
return res.send(JSON.stringify({success : "Updated Successfully", status : 200}));
答案 1 :(得分:0)
问题似乎与async await fetch api有关,await的问题破坏了服务器的响应。