JSONP cross-domain

Source: Internet
Author: User

because AJAX requests do not support cross-domain, there is a problem with multiple domain names interacting. The principle of cross-domain is this, the SRC attribute in HTML request the address can be cross-domain, for example and <script>For example, test.js in Test ({name:'Meigong', Sex:'Mans'}); in index.html<script >function test (data) {alert ('Name:'+data.name+"Gender:"+data.sex); }    </script> <script src='Http://www.biuman.comt/test/test.js'></script>This time will pop box, jailbreak success! Here is a package, the callback function name passed past imitation Baidu<script>//callback functionfunction Test (a) {alert (a.name); } setTimeout (function () {varURL ="http://www.biuman.com/test/jsonp/test.php?cb=test"; varScript =document.createelement ('Script'); Script.setattribute ('src', URL); document.getElementsByTagName ("Body")[0].appendchild (script); }, -); </script >test.php<?php $filename='./su'; $fun= $_get['CB']; $arr=Array ('name'='Meigong',  'Sex'='Mans'  ); $res=Json_encode ($arr); $res= $fun." (". $res.")";  File_put_contents ($filename, $res); Header ('content-type:biuman/test'); Header ('content-disposition:attachment; filename='. $filename);//download mode, Firebug in the network in response to not see the contentReadFile"$filename");  Exit (); ? >In addition, jquery encapsulates the Jsonp<script >$ (function () {$.ajax ({URL:"http://www.biuman.com/test/jsonp/test.php", DataType:"Jsonp", Jsonp:"CB",//           //The parameter name that is passed to the request handler or page to obtain the name of the JSONP callback function (generally by default: callback)Jsonpcallback:"Test",//Required callback functionsuccess:function (data) {alert (data.name); }, Error:function () {alert ('Network Exceptions');  }      }); })  </script >

<script type= "Text/javascript" src= "Http://sources.ikeepstudying.com/js/jquery-1.8.3.min.js" ></script >
<script type= "Text/javascript" >
$.getjson ("http://test.ikeepstudying.com/json.php?callback=?",
function (Result) {
for (var i in result) {
Alert (i+ ":" +result[i]);//loop output a:1,b:2,etc.

JSONP cross-domain

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.