This time I will bring you a detailed graphic explanation of Nginx's implementation of AJAX cross-domain requests (with code). What are theprecautions for Nginx's implementation of AJAX cross-domain requests?. Here are practical cases. Let's take a look. .
AJAX requests from one domain to another domain will have cross-domain problems. So how to implement ajax cross-domain request on nginx? To enable cross-origin requests on nginx, you need to add the add_header Access-Control* directive. As shown below:
location /{ add_header 'Access-Control-Allow-Origin' 'http://other.subdomain.com'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET'; ... ... the rest of your configuration here ... ... }
Commentsare as follows:
First command: Authorize requests from other.subdomain.com
Second command : When this flag is true, whether the response to the request can be exposed
Third Day Directive: Specify the method of the request, which can be GET, POST, etc.
If you need to allow from any domain Access can be configured like this:
Access-Control-Allow-Origin: *
Restart nginx
service nginx reload
ajax cross-domain request test
When successful, the response header is as follows:
HTTP/1.1 200 OK Server: nginx Access-Control-Allow-Origin: other.subdomain.com
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
What problems will you encounter when using get and post in Ajax and how to deal with it
$. How to use the parameters of the Ajax() method
The above is the detailed content of Detailed graphic explanation of Nginx implementing AJAX cross-domain request (with code). For more information, please follow other related articles on the PHP Chinese website!