2017-02-21 32 views
3

观看时,帖子的内容类型正在发生变化我有一个取其中的请求类型似乎正在改变它搞乱了我的职务。我提交我的基本表格(仅限一个字段)。这是抓取。作出反应 - 在获取提琴手

 handleSubmit(event, data) { 
    //alert('A name was submitted: ' + this.state.value); 
    event.preventDefault(); 
    console.log("SUBMIT STATE::", this.state.value); 
    return (
     fetch("//localhost:5000/api/values/dui/", { 
      method: "post", 
      mode: 'no-cors', 
      headers: { 
       'Access-Control-Allow-Origin': '*', 
       'Content-Type': 'application/json', 
       'Accept': 'application/json',     
      }, 
      body: JSON.stringify({ 
       name: this.state.value, 
      }) 
     }).then(response => { 
      if (response.status >= 400) { 
       this.setState({ 
        value: 'no greeting - status > 400' 
       }); 
       throw new Error('no greeting - throw'); 
      } 
      return response.text() 
     }).then(data => { 
      var myData = JSON.parse(data); 
      this.setState({ 
       greeting: myData.name, 
       path: myData.link 
      }); 
     }).catch(() => { 
      this.setState({ 
       value: 'no greeting - cb catch' 
      }) 
     }) 
    ); 


} 

但是,当我在提琴手内容类型看这是现在“内容类型:文本/无格式;字符集= UTF-8”。这里是原始的Fiddler:

POST http://localhost:5000/api/values/dui/ HTTP/1.1 
Host: localhost:5000 
Connection: keep-alive 
Content-Length: 16 
accept: application/json 
Origin: http://evil.com/ 
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 

内容类型:文本/无格式;字符集= UTF-8 的Referer:http://localhost:3000/ 接受编码:gzip,放气,BR 接受语言:EN-US,烯; q = 0.8

{"name":"molly"} 

在DOM查看我只看到:

POST http://localhost:5000/api/values/dui/ 415(不支持的媒体类型)

我还发现奇怪的是,'接受'是小写以及'内容类型'。任何理由为什么发生这种情况。我还没有发现任何具体的搜索结果。

回答

3

当为请求设置了mode: 'no-cors'时,浏览器将不允许您设置除CORS-safelisted request-headers以外的任何请求标头。见the spec requirements about adding headers

要附加一个名称/值(名称/)对到Headers对象(),执行这些步骤:

  • 否则,如果后卫是 “request-no-cors” 和/不是CORS-safelisted request-header,回报。
  • 在这种算法中,return等同于“无添加该标头的标头的对象返回”。

    而且它而不致设置为text/plain;charset=UTF-8的原因是因为the algorithm for the request constructor电话为extract a body algorithm包括以下步骤:

    接通对象的类型:

    USVString

    • 套装Content-Typetext/plain;charset=UTF-8
    1

    因此,这是解决这个问题,我切换到“无CORS”到“CORS”。坦白说,我认为我已经翻转以失败告终,这些之前,因为跨起源问题,我是在我的本地开发工作站,我被部署到服务器,但不用之间不得不说,当我设置回模式:“CORS”,一切重新工作。本地工作站和服务器。为什么这会改变实际的请求标题,林不知道。如果任何人有答案,我会很乐意上传。

    谢谢。