JQuery ajax get support cross-domain approach, using jsonp to complete. There are two cross-domain requests complete manner. One is to use the bottom Api Jquery ajax cross-domain request, while the other is advanced JQuery ajax package.
First, the use Jquery ajax way
$.ajax({
url:'http://www.mysite.com/demo.do', // 跨域URL
type:'get',
async:false,
dataType:'jsonp',
jsonp: 'jsoncallback', //默认callback
data: mydata,
time:5000,
beforeSend:function(){
//jsonp 方式此方法不被触发。原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了
},
success:function(data){
//客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,会动态执行这个callback函数
if(json.actionErrors.length!=0){
alert(json.actionErrors);
}
genDynamicContent(qsData,type,json);
},
complete: function(XMLHttpRequest, textStatus){
$.unblockUI({ fadeOut: 10 });
},
error: function(xhr){
//jsonp 方式此方法不被触发
//请求出错处理
alert("请求出错(请检查相关度网络状况.)");
}
});
Second, the use JQuery getJSO way
$.getJSON("请求的路径=?参数",
function(json){ //返回的结果
if(json.属性名==值){
// 执行代码
}
});
The basic principle is that Jsonp: add a dynamic consistent. Jsonp is injecting a script (Script InJection) behavior, so it will be some security issues.
同时需要注意Jsonp是不支持post跨域请求的。