Home  >  Article  >  Backend Development  >  Nginx implements AJAX cross-domain requests

Nginx implements AJAX cross-domain requests

WBOY
WBOYOriginal
2016-08-08 09:30:271368browse

More: http://www.webyang.net/Html/web/article_135.html

This is how HTTP cross-domain requests are implemented in the latest W3C standard,

Cross-Origin Resource Sharing
To put it simply, it is The cross-domain target server returns a series of headers, and these headers are used to control whether to agree to cross-domain.
These Headers are:
4 Syntax
4.1 Access-Control-Allow-Origin HTTP Response Header
4.2 Access-Control-Max-Age HTTP Response Header
4.3 Access-Control-Allow-Credentials HTTP Response Header
4.4 Access-Control- Allow-Methods HTTP Response Header
4.5 Access-Control-Allow-Headers HTTP Response Header
4.6 Origin HTTP Request Header
4.7 Access-Control-Request-Method HTTP Request Header
4.8 Access-Control-Request-Headers HTTP Re quest Header
is here There are some in the Request package and the Response package.
The most sensitive one is the Access-Control-Allow-Origin Header, which is used in the W3C standard to check whether the cross-domain request can be passed. (Access Control Check)
So if you need to cross domains, the solution is to add Access-Control-Allow-Origin to the header of the resource to specify the domain you authorize. It doesn’t matter to me here, just specify the asterisk *, and any domain can access me H.

The specific operation method can be controlled through different entrances:

1. PHP code control:
  1. php
  2. header("Access-Control-Allow-Origin: *");
  3. ?>
2. HTML header control:
  1. http-equiv="Access-Control-Allow-Origin"content="*">
3. nginx configuration:
  1. location /{
  2. add_header Access-Control-Allow-Origin*;
  3. }
saw on the Internet that someone would add three sentences to nginx.conf:
  1. #授权从other.subdomain.com的请求
  2. add_header 'Access-Control-Allow-Origin''http://other.subdomain.com';
  3. #当该标志为真时,响应于该请求是否可以被暴露
  4. add_header 'Access-Control-Allow-Credentials''true';
  5. #指定请求的方法,可以是GET,POST等
  6. add_header 'Access-Control-Allow-Methods''GET';

But I tested it myself and it’s not necessary. In addition, if you want to specify a specific domain name, just modify *, which is usually the second-level domain name of an open site.

The above introduces Nginx to implement AJAX cross-domain requests, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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