大佬教程收集整理的这篇文章主要介绍了使用JSONP对JSON文件进行跨域Ajax请求,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您的服务必须返回Jsonp,这基本上是JavaScript代码。您需要从AJAX请求中为服务提供回调函数,然后返回的是函数调用。
下面是一个工作示例。
AJAX请求:
$.AJAX({
crossDomain: true,
type:"GET",
ContentType: "application/Json; charset=utf-8",
async:false,
url: "http://<your service url here>/HelloWorld?callBACk=?",
data: {projectID:1},
dataType: "Jsonp",
JsonpCallBACk: 'fnsuccesscallBACk'
});
服务器端代码返回Jsonp(c#):
public voID HelloWorld(int projectID,String callBACk)
{
String s = "Hello World !!";
StringBuilder sb = new StringBuilder();
JavaScriptserializer Js = new JavaScriptserializer();
sb.Append(callBACk + "(");
sb.Append(Js.serialize(s));
sb.Append(");");
Context.Response.Clear();
Context.Response.ContentType = "application/Json";
Context.Response.Write(sb.ToString());
Context.Response.End();
}
我想从domain2(example2.com)访问位于domain1(example.com)中的JSON文件。例如,
$.ajax({
type:'get',url: 'http://example.com/vigneshmoha.json',success: function(data) {
console.log(data);
},statusCode: {
404: function() {
console.log('Status code: 404');
}
}
});
我想从其他网域(例如example2.com)向example.com提出这个Ajax请求。
我已经尝试过JSONP。我不明白它是如何工作的。有人可以解释其工作方式吗?
以上是大佬教程为你收集整理的使用JSONP对JSON文件进行跨域Ajax请求全部内容,希望文章能够帮你解决使用JSONP对JSON文件进行跨域Ajax请求所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。