[Ajax] Settings Access-control-allow-origin implementation Cross-domain Access (Header) __ajax

Source: Internet
Author: User

Ajax Cross-domain Access is an old problem, there are many solutions, more commonly used is the Jsonp method, the Jsonp method is an unofficial method, and this method only supports get way, not as safe as post method.


Even if you use the Jsonp method of jquery, type is set to post and automatically becomes get.


Official Problem Description:

"Script": evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, ' _=[timestamp] ', to the URL unless the cache option is set to True . Note:this'll turn POSTs into GETs for Remote-domain requests.


If you use the Post method across domains, you can use the Create a hidden iframe to do the same as the Ajax upload picture principle, but this will be more cumbersome.


Therefore, it is simpler to implement Cross-domain access by setting Access-control-allow-origin.


For example: The domain name of the client is www.client.com, and the requested domain name is www.server.com

If you use AJAX access directly, you will have the following error

XMLHttpRequest cannot load http://www.server.com/server.php. No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' http://www.client.com ' is therefore not allowed access.


join in the requested response header

[PHP] view Plain copy//Specify allow other domain name Access header (' access-control-allow-origin:* ');   Response Type header (' Access-control-allow-methods:post '); Response header Set header (' Access-control-allow-headers:x-requested-with,content-type ');

You can implement Ajax post Cross-domain access.


The code is as follows:

client.html Path: http://www.client.com/client.html

[HTML]  View Plain  copy   <! doctype html public  "-//w3c//dtd html 4.0 transitional//en" >   < html>     server.phpPath: http://www.server.com/server.php

[PHP] view plain copy

Related Article

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.