jquery, JavaScript implementation (GET, post two ways) cross-domain solution

Source: Internet
Author: User

First, implement get method cross-domain Request data

Browser-side

<Script>$ (document). Ready (function() {$.ajax ({URL:"http://www.xxx.cn/index.php", type:"Get", DataType:"Jsonp", //Specifies the type of data returned by the serverSuccess:function(data) {Console.log (data); }    });});</Script>

Server-side

<?php$data = [' info ' =  ' cross-domain request succeeded ', ' status ' = 1]; $callback = $_get[' callback '];// The callback parameter is a callback parameter that initiates a request that is jquery. echo $callback. ' ('. Json_encode ($data). ') '; /All return data is required in this manner.?>

Second, the implementation of the post way cross-domain request

Browser-side

<script>$ (document). Ready (function () {$.ajax ({    URL: "http://www.xxx.cn/index.php",    type: "POST",    dataType: "JSON",//Specifies the type of data returned by the server    success:function (data) {        console.log (data);});}    ); </script>

Server-side

access-control-allow-origin:* that allows any domain name to be accessed across domains

If you need to specify a domain name to allow cross-domain access, simply change the access-control-allow-origin:* to access-control-allow-origin: allowed domain name

For example: Header (' access-control-allow-origin:http://www.xxx.com ');

<?phpheader (' access-control-allow-origin: * ');//Allow all sources to access the header (' access-control-allow-methods:post ');//Response type $ data = [' info ' +  ' post method, cross domain request succeeded ', ' status ' = 1];echo Json_encode ($data);? >

Some header request headers

Specify allow other domain names to be accessed
Header (' access-control-allow-origin:* ');
Response type
Header (' Access-control-allow-methods:post ');
Response Header Settings
Header (' Access-control-allow-headers:x-requested-with,content-type ');

//设置内容类型为json

Header (' Content-type:application:json;charset=utf8 ');

jquery, JavaScript implementation (GET, post two ways) cross-domain solution

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.