我有一个消息方法,当我单击表单上的提交按钮时会触发该方法。但是我无法访问我的 Node rest api 中的 url,我试图通过返回未定义的“req.body.url”访问 url,当我看到 req.body 中没有任何内容时。我已经使用 axios 完成了它,它工作正常帮助我使用 fetch
message(event){
event.preventDefault();
const uri = this.state.short.url;
fetch("http://localhost:3000/messages", {
method: "post",
body: {url: uri}
})
.then(function(data) {
console.log("Request succeeded with response", data);
})
.catch(function(error) {
console.log("Request failed", error);
});
}
最佳答案
在您的获取中,您需要指定 Json 并像这样对您的 json 进行字符串处理:
body: JSON.stringify({ url: uri }),
headers:{
'Content-Type': 'application/json'
}
关于javascript - 使用 fetch api 在 react 中发帖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51997775/