本文实例分析了jquery中ajax跨域。分享给大家供大家参考,具体如下:
JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问
方法一: jsonp之 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
二jsonp之$.ajax
jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。
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
"sdfg","aa"=>23);
$jsondata=json_encode($ary);
echo $_GET['callback5'].'('.$jsondata.')';
?>
希望本文所述对大家jQuery程序设计有所帮助。










