The server allows other domain names to access data across domains by setting the response header.

Source: Internet
Author: User

Set the response header:

# Cross-origin domains are allowed. * indicates that all domains receive

Access-control-allow-origin: http://foo.example

# Methods that allow cross-origin execution

Access-control-allow-Methods: Post, get, options

# Header information that allows cross-origin settings

Access-control-allow-headers: X-PINGOTHER

The following is the nginx method
if ($ request_method = 'options ') {
add_header access-control-allow-origin *;
add_header access-control-allow-Credentials true;
add_header access-control-allow-methods 'get, post, options';
add_header 'access-control-allow-headers' 'DNT, x-Mx-reqtoken, keep-alive, User-Agent, X-requested-with, if-modified-since, cache-control, Content-Type ';
return 200;
}< br> if ($ request_method = 'post') {

Add_header 'access-control-allow-origin '*;
Add_header 'access-control-allow-Credentials ''true ';
Add_header 'access-control-allow-methods ''' get, post, options ';
Add_header 'access-control-allow-headers' DNT, X-Mx-reqtoken, keep-alive, User-Agent, X-requested-with, if-modified-since, cache-control, Content-Type ';

}

If ($ request_method = 'get '){

Add_header 'access-control-allow-origin '*;
Add_header 'access-control-allow-Credentials ''true ';
Add_header 'access-control-allow-methods ''' get, post, options ';
Add_header 'access-control-allow-headers' DNT, X-Mx-reqtoken, keep-alive, User-Agent, X-requested-with, if-modified-since, cache-control, Content-Type ';

}

Javascript: Call An Instance

Set the above configuration on nginx of dev.int. weishi. mobil. The website to which the Javascript page belongs is www.mongoach.com, and call the data on dev.int. weishi. Mobil through cross-origin post.

CallCodeYes
Postdata: function (data, success ){
VaR accounturl = "http://dev.int.weishi.mobi/web" + rpc. urluser;
VaR jsonuserinfo = $. tojson (data );
Jquery. Ajax ({
Type: 'post ',
Contenttype: 'application/json ',
URL: accounturl,
Data: jsonuserinfo,
Datatype: 'json ',
Success: function (data ){
If (data. Code = 0 ){
Success (data. Result );
} Else {
Alert (data. msg );
}
},
Error: function (data ){
Alert ("error ")
}
});
},

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.