JS spanning request 2 implemented JSONP/backend interface settings run across headers

Source: Internet
Author: User

Because of the browser-homologous policy, a domain name of JS to the B domain name Ajax request will be banned. JS implementation across the Access interface has 2 methods.

1. The backend interface is set to allow spanning header headers.

Header (' access-control-allow-origin:* ');  Support for full domain access, insecure, after deployment requires a fixed limit to the client URL header (' access-control-allow-origin:http://www.example-a.com ');   The URL with the protocol is set, not a domain name. You can set multiple URLs, separating echo json_encode with commas ([' name ' = ' Lucy ']);}

  

  

The client will be able to request normal Ajax.

2. The client passes the JSONP request, the server does not have to set across the header. About JSONP The principle is no longer said here, you can find information separately. Here is a concrete way to implement it.

client requests, as examples of jquery implementations

<script type= "Text/javascript" >    $ (document). Ready (function () {        $.ajax ({            type: "Get",            async: False,            URL: "http://www.example-a.com/api.php?id=1",            dataType: "Jsonp",            Jsonp: "Callback",//Request PHP parameter name            //jsonpcallback: "Jsonhandle",//callback function to execute, (in jquery, this parameter can be omitted, jquery randomly generates a name)            success:function (data) {                console.log (data);});    }); </script>

api.php

<?php     //Complete business logic to get Data     echo $_get[' callback ']. ' ('. Json_encode ($data). ') ';  $_get[' callback ', which is the ' Jsonp ' parameter of Jsonp request in jquery above
?>

JS spanning request 2 implemented JSONP/backend interface settings run across headers

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.