search

Home  >  Q&A  >  body text

javascript - fetch settings ('Content-Type', 'application/json') error

Fetch API cannot load http://172.16.37.174/index.php?r=api/UserLogin. Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

I want to pass the parameter json string to the background and set the headers. No matter whether I add a new header or add it directly to the headers, this error will be reported. The background is PHP. Does anyone know what's going on?

Solution:

(1)之前后台在设置跨域问题的时候没有加单引号,eg:add_header Access-Control-Allow-Origin *;
(2)没有加 add_header 'Content-Type' 'application/json;charset=utf-8';
现在代码设置如下:
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Content-Type' 'application/json;charset=utf-8';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 

The problem is solved.

仅有的幸福仅有的幸福2752 days ago782

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:40:19

    Pay attention to the type correspondence between content-type and body. It should be an error caused by type mismatch.
    Different content-types will require you to pass different types of values ​​
    I forgot the details, you can try it
    body: param
    body: 'name=aaa&age=111'

    reply
    0
  • 黄舟

    黄舟2017-05-19 10:40:19

    Please go to the search engine to find the answer yourself before asking a question, and don’t increase the number of people on the Internet, otherwise there is a high chance that no one will ask your question.

    reply
    0
  • Cancelreply