Home > Backend Development > PHP Tutorial > javascript - js跨域请求问题

javascript - js跨域请求问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:10:11
Original
970 people have browsed it

我在后端已经添加了header("Access-Control-Allow-Origin:*");,但是为什么前端使用js请求时还是出现

<code>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://nichijou.org/x. (Reason: CORS header 'Access-Control-Allow-Origin' missing). 
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://nichijou.org/x. (Reason: CORS request failed).</code>
Copy after login
Copy after login

回复内容:

我在后端已经添加了header("Access-Control-Allow-Origin:*");,但是为什么前端使用js请求时还是出现

<code>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://nichijou.org/x. (Reason: CORS header 'Access-Control-Allow-Origin' missing). 
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://nichijou.org/x. (Reason: CORS request failed).</code>
Copy after login
Copy after login

  1. 很多浏览器都已经不支持使用 * 来设置 Access-Control-Allow-Origin 了,正常的做法是 header("Access-Control-Allow-Origin:".$_SERVER['HTTP_HOST']);

  2. 如果需要传递 COOKIE 的话还需要搭配 Access-Control-Allow-Credentials: true 这个头使用。具体可以看文档:https://developer.mozilla.org/en-US/docs/Web/HTTP/Server-Side_Access_Control

什么浏览器啊,版本号都列出来

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template