简体   繁体   English

为什么 header 中的内容类型仍然是 text/plain 虽然我将其设置为 application/json

[英]Why content-type in header remains text/plain though I set it as application/json

I use fetch() to post json data as below我使用 fetch() 发布 json 数据如下

var data = {
            name: this.state.name,
            password: this.state.password
        }
fetch('http://localhost:3001/register/paitent', {
            method: 'POST',
            body: JSON.stringify(data), 
            mode: 'no-cors',
            headers: new Headers({
                'Content-Type': 'application/json'
            })
        })
        .then(res => res.json())

I always get {} in request.body in my express server router, and I figure out the problem is in sending request, it's always 'text/plain'.我总是在我的快速服务器路由器的 request.body 中得到 {},我发现问题出在发送请求中,它总是“文本/纯文本”。 Why this happens?为什么会发生这种情况?

在此处输入图像描述

You set the mode to no-cors . 您将模式设置为no-cors

Setting the Content-Type to application/json requires permission from CORS… 将Content-Type设置为application/json需要获得CORS的许可…

The only allowed values for the Content-Type header [without triggering a preflight CORS request] are: Content-Type标头(不触发预检CORS请求)的唯一允许值为:

  • application/x-www-form-urlencoded 应用程序/ x-WWW窗体-urlencoded
  • multipart/form-data 多部分/格式数据
  • text/plain 纯文本/

mdn mdn

…which you told fetch not to ask for. ......你告诉fetch不问。 Since you don't ask for permission, you don't get permission, so fetch reverts to text/plain . 由于您不请求许可,因此您也没有获得许可,因此访存将恢复为text/plain

I was stuck on same issue for hours now figure out exact solution for this as mentioned in documentation of expressjs @Quentin has pointed towards exact issue.我被困在同一个问题上几个小时现在找出确切的解决方案,正如 expressjs @Quentin 的文档中提到的那样,已经指出了确切的问题。 here i am posting some more details may it will help someone else to resolve issue quickly.在这里,我发布了更多详细信息,可能会帮助其他人快速解决问题。

When you are posting json object with 'Content-Type':'application/json' fetch or any other method browser will request for cors setting first for security reason then if it receives cors options which is very easy to achieve. When you are posting json object with 'Content-Type':'application/json' fetch or any other method browser will request for cors setting first for security reason then if it receives cors options which is very easy to achieve. then it will post original object to server as nodejs server is waiting for 'Content-Type':'application/json'然后它将原始 object 发布到服务器,因为 nodejs 服务器正在等待 'Content-Type':'application/json'

you can check documention of cors setting on expressjs.com您可以在 expressjs.com 上查看 cors 设置的文档

you need to just install cors package and add this line in your file您只需安装 cors package 并将此行添加到您的文件中

app.options("/data",cors()); app.options("/data",cors());

this will setup preflight cors response for your browser for specific path /data then your method will look like this 'app.post('/data',cors(),function(req,res){}'这将为您的浏览器设置针对特定路径/数据的预检 cors 响应,然后您的方法将如下所示 'app.post('/data',cors(),function(req,res){}'

Now you can post data with 'Content-Type':'application/json'现在您可以使用 'Content-Type':'application/json' 发布数据

i will recommend to read cors article on MDN for better understanding我建议阅读 MDN 上的 cors 文章以便更好地理解

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将响应标头“ Content-Type”设置为“ application / json”时,HTTP响应正文可以为空吗? - Can HTTP response body be empty when response header 'Content-Type' is set to 'application/json'? 无法将标题Content-Type更改为application / json - Unable to change header Content-Type to application/json 使用 Fetch API javascript 将 Content-Type 设置为 application/Json - set Content-Type to application/Json using Fetch API javascript Google Drive API 响应中不需要的“content-type: text/plain;charset=UTF-8” header - Unwanted “content-type: text/plain;charset=UTF-8” header in Google Drive API Response 如何在$ resource AngularJS中将内容类型设置为DELETE方法的application / json - How can I set the content-type as application/json for DELETE method in $resource AngularJS HTTP 内容类型标头和 JSON - HTTP Content-Type Header and JSON 设置Content-Type标头为多部分? - Set Content-Type header to multipart? 应该什么时候Content-Type是application / json - When should Content-Type be application/json npm请求同时发送令牌和标头“ content-type”:“ application / json” - npm request send token and header 'content-type': 'application/json' at the same time 当我的dataType:“ JSON”时,为什么我的Ajax请求发送“内容类型:application / x-www-form-urlencoded”? - Why is my Ajax request sending “Content-Type: application/x-www-form-urlencoded” when I have dataType: “JSON”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM