This article analyzes ajax cross-domain in jquery through examples. Share it with everyone for your reference, the details are as follows:
JSONP is an unofficial protocol that allows integrating Script tags on the server side and returning them to the client, enabling cross-domain access in the form of javascript callback
Method 1: jsonp's getJSON
js
var url = "http://localhost/mytest/jsonp_php.php?callback=?"; $.getJSON(url, { "age": 21, "name": "kitty" }, function (data) { alert("name:" + data.name + ", age:" + data.age); });
php
<?php $age=$_GET["age"]; $name=$_GET["name"]; $jsondata = "{age:$age, name:'$name'}"; echo $_GET['callback'].'('.$jsondata.')'; ?>
Two jsonp of $.ajax
js
$.ajax({ type: 'GET', url: 'http://localhost/mytest/jsonp_php.php', dataType: "jsonp", jsonp: "callback5", jsonpCallback:"flightHandler", data: { "age": 21, "name": "kitty" }, success: function (data) { alert("name:" + data.sd + ", age:" + data.aa) } })
php
<?php $age=$_GET["age"]; $name=$_GET["name"]; $ary=array("sd"=>"sdfg","aa"=>23); $jsondata=json_encode($ary); echo $_GET['callback5'].'('.$jsondata.')'; ?>
I hope this article will be helpful to everyone in jQuery programming.