Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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/9/three.js/2.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
Http 使用fetch发送和接收文本/纯文本的正确方式_Http_Fetch - Fatal编程技术网

Http 使用fetch发送和接收文本/纯文本的正确方式

Http 使用fetch发送和接收文本/纯文本的正确方式,http,fetch,Http,Fetch,使用fetch发布和接收纯文本的正确语法是什么 这是我的代码: fetch('http://localhost:3000/postendpoint', { method: 'POST', mode: 'no-cors', headers: { 'Content-Type': 'text/plain' }, body: 'hello world' }).then(function(response) {

使用fetch发布和接收纯文本的正确语法是什么

这是我的代码:

fetch('http://localhost:3000/postendpoint', {
      method: 'POST',
      mode: 'no-cors',
      headers: {
          'Content-Type': 'text/plain'
      },
      body: 'hello world'
      }).then(function(response) {
        return response.text()
      }).then(function(data) {
        console.log("data ", data)
      }).catch(function(err){ 
         console.log("An error occurred")
      });
当我使用curl时,应用程序以文本回复,没有任何问题,我可以在开发人员控制台中看到一切正常,但我的应用程序不会记录任何内容。我只得到:

data
但之后没有实际文本。

不使用cors意味着您将无法读取响应正文。这是故意的,不要使用无cors


如果你想了解更多的背景,我写了这样一句话:

非常感谢。这是一个惊人的反应。