Javascript 我可以使用fetch-in-react发布文本数据吗
我正在使用react js,我想发布文本并返回文本 有人能帮我发短信和接收短信吗?我用过Javascript 我可以使用fetch-in-react发布文本数据吗,javascript,node.js,reactjs,Javascript,Node.js,Reactjs,我正在使用react js,我想发布文本并返回文本 有人能帮我发短信和接收短信吗?我用过 内容类型text/plain,但这没有帮助 有什么办法吗 const options = { method: "POST", headers: { "Content-Type": "text/plain" }, body: this.state.url } fetch("http://localhost:3000/messages", options)
内容类型text/plain
,但这没有帮助
有什么办法吗
const options = {
method: "POST",
headers: {
"Content-Type": "text/plain"
},
body: this.state.url
}
fetch("http://localhost:3000/messages", options)
.then(response => response)
.then(data => {
console.log(data)
this.setState({
code: data
});
});
这就是我试图从api中获取文本值的内容
我得到一个错误,因为
无法获取未捕获的承诺类型错误
fetch
为响应对象返回一个“promise”,该对象根据内容类型具有json、文本等的promise创建者。所以代码应该改为
也可以考虑在错误的情况下添加一个catch块,并检查控制台输出错误(如果有的话)。
发往哪里?接受什么?您想做什么?请在您的问题中更加具体。请阅读有关进行api调用的内容
const options = {
method: "POST",
headers: {
"Content-Type": "text/plain"
},
body: this.state.url
}
fetch("http://localhost:3000/messages", options)
.then(response => response.json()) // or response.text()
.then(data => {
console.log(data)
this.setState({
code: data
});
})
.catch(err => { console.log('error while fetching', err) });