javascript - Fetch API - Content-Type 在设置为 application/json 时以 text/plain 形式发送
问题描述
在我的 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))
当我检查开发人员工具下的网络选项卡中的条目时,我看到 Content-Type 是text/plain
,但我不明白为什么将其设置为application/json
.
我尝试Edit and send
在请求中使用该选项并替换text/plain
为,application/json
但在发送后它被发送为text/plain
.
解决方案
mode:"no-cors" 选项是罪魁祸首。
删除该选项,它应该可以工作
要修复它,请传递一个新的 headers 对象
headers: new Headers({'content-type': 'application/json'})
推荐阅读
- rest - API 版本控制:访问最新版本的 URL 路径
- devops - RancherOS + K8s 在具有多个节点的单个物理机上
- scala - 无法使用 presto 将 Spark 的 DataFrame 写入配置单元
- javascript - 有什么方法可以绕过 Fortinet 阻止 Web 内容?
- mysql - 为什么mysql @@variables 与my.cnf 文件的内容不匹配?
- reactjs - 使用 Azure App Service 扩展部署 React App 时出现“错误:编写 ECONNABORTED”
- lua - Lua表差异键功能
- java - 如何从 Firebase 中同时检索字符串和列表
- c# - 如何在 Asp.Net Core MVC 中进行自定义模型绑定
- javascript - 如何在按钮单击时验证输入不保存 0 或小于 0?