Home >Web Front-end >JS Tutorial >Detailed graphic explanation of Nginx implementing AJAX cross-domain request (with code)

Detailed graphic explanation of Nginx implementing AJAX cross-domain request (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-04-04 11:47:161866browse

This time I will bring you a detailed graphic explanation of Nginx's implementation of AJAX cross-domain requests (with code). What are the precautions 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
...
...
}

Comments are 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!

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