PHP Ajax the best solution for cross-domain problems

Source: Internet
Author: User

First, this article by setting Access-control-allow-originTo implement cross-domain.

For example: The domain name of the client is, and the requested domain name is

If you use AJAX access directly, you will get the following error:

XMLHttpRequest cannot load No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' ' is therefore not allowed access.

1. Allow single domain access

To specify a domain name ( for cross-domain access, simply add the following code to the header of the file:header(' Access-control-allow-origin: ');

2. Allow multiple domain names to be accessed
To specify multiple domain names (,, and so on) for cross-domain access, simply add the following code to the file header:
$origin=Isset($_server[' Http_origin '])?$_server[' Http_origin '] : ; $allow _origin Span class= "pun" >= Array ( ,  ' '  );  if (in_array (  $origin , $allow _origin Header ( ' access-control-allow-origin: '  $origin );  } 
3. Allow all domain names to access

To allow all domain access, simply add the following code to the header of the file:header(' access-control-allow-origin:* ' );

Second, Ajax initiated Jsonp cross-domain request

We can realize the cross-domain Ajax request through the Jsonp way of jquery, the service side PHP also needs to make corresponding processing, that is to say PHP this side must and front page to request and return data according to certain format.

Example: Front-end page initiates a JSONP request:

12345678910111213 $.ajax({  type: "get",  data: "random="+Math.random(),  url: "",  dataType: "jsonp",  jsonp: "callback",  success: function(data) {   console.log(data);  },  error: function() {   console.log(‘Request Error.‘);  }});

We will find that there are dataType in the AJAX request parameters: "Jsonp" and Jsonp: "Callback", which indicates that I want to request JSONP, and there will be a callback callback return. Of course, we can also customize callback functions, such as Jsonpcallback: "Success_jsonpcallback"

can also be simply written as:

12345 jQuery.getJSON(‘",{ random: Math.random()}, function(data){ console.log(data);});

The PHP backend service code can be written like this (note the format returned by the output):

12345 $data = array(  ‘rand‘ => $_GET[‘random‘],  ‘msg‘ => ‘Success‘);echo $_GET[‘callback‘].‘(‘.json_encode($data).‘)‘;

PHP Ajax the best solution for cross-domain problems

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