Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我可以使用fetch-in-react发布文本数据吗_Javascript_Node.js_Reactjs - Fatal编程技术网

Javascript 我可以使用fetch-in-react发布文本数据吗

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)

我正在使用react js,我想发布文本并返回文本

有人能帮我发短信和接收短信吗?我用过
内容类型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) });