我在请求类型似乎正在更改的地方进行提取,这弄乱了我的帖子。我提交基本表格(仅一个字段)。这是提取。
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'
})
})
);
}
但是当我在提琴手中查看内容类型时,内容类型现在是'内容类型:text / plain; charset = UTF-8'。这是原始的提琴手:
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引荐来源:http://localhost:3000/接受编码:gzip,deflate,br接受语言:en-US,en; q = 0.8
{"name":"molly"}
在DOM Inspector中,我只看到:
POSThttp://localhost:5000/api/values/dui/415(不支持的媒体类型)
[我也发现'accept'是小写字母以及'content-type'也是很奇怪的。发生这种情况的任何原因。我尚未在搜索中找到任何具体内容。
[为请求设置mode: 'no-cors'
时,浏览器将不允许您设置CORS-safelisted request-headers以外的任何请求标头。参见the spec requirements about adding headers:
要将名称/值(名称 / 值)对附加到
Headers
对象(标题),请运行以下步骤:
- 否则,如果guard是“
request-no-cors
”且name / value不是CORS-safelisted request-header,则返回。
在该算法中,return
等同于“在不将标题添加到Headers对象的情况下返回”。
并且之所以将其设置为text/plain;charset=UTF-8
的原因是the algorithm for the request constructor调用了extract a body algorithm,其中包括以下步骤:
打开对象的类型:
↪USVString
- 将
Content-Type
设置为text/plain;charset=UTF-8
。
所以这就是解决此问题的方法,我将'no-cors'切换为'cors'。坦率地说,由于本地开发工作站和要部署到的服务器之间存在跨源问题,我以前曾把这些触发器放过,但是不用说,当我将其设置回“ cors”模式时,它们又能正常工作了。本地工作站和服务器。我不确定为什么要更改实际的请求标头。如果有人有答案,我会很乐意投票。
谢谢。