aba*_*edo 13 javascript fetch-api
在我的 React 应用程序中,当我提交表单来创建新用户时,我使用fetch.
代码是这样的:
fetch('http://0.0.0.0:3000/users', {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(response => console.log(response))
Run Code Online (Sandbox Code Playgroud)
当我检查开发人员工具下的网络选项卡中的条目时,我看到内容类型是,text/plain但我不明白为什么如果我将其设置为application/json.
Edit and send我已尝试使用请求中的选项并替换text/plain为,application/json但在发送后,它被发送为text/plain.
var*_*ons 13
mode:"no-cors" 选项是罪魁祸首。
删除该选项,它应该可以工作
要修复它,请传递一个新的 headers 对象
headers: new Headers({'content-type': 'application/json'})
Run Code Online (Sandbox Code Playgroud)