Jsonp的用法非常简单,不过需要服务器端和客户端同时支持。
服务器端
string callback = Request["callback"];
string response = string.Format("\"value1\":\"{0}\",\"value2\":\"{1}\", v1, v2);
string call = callback + "({" + response + "})";
Response.Write(call);
string response = string.Format("\"value1\":\"{0}\",\"value2\":\"{1}\", v1, v2);
string call = callback + "({" + response + "})";
Response.Write(call);
客户端
这里使用JQ,有下面两种调用方法:
1、使用getJson
var url = "xxx.aspx?p1=1&p2=2&callback=?";
jQuery.getJSON(url, function(data) {
alert("value1:" + data.value1+ " value2:" + data.value2);
});
jQuery.getJSON(url, function(data) {
alert("value1:" + data.value1+ " value2:" + data.value2);
});
2、使用ajax
jQuery.ajax({
type:"get",
url: "xxx.aspx",
dataType : "jsonp",
jsonp: "callback",
data: "p1=1&p2=2&callback=?",
success:function(msg){
alert("value1:" + data.value1+ " value2:" + data.value2);
}
});
type:"get",
url: "xxx.aspx",
dataType : "jsonp",
jsonp: "callback",
data: "p1=1&p2=2&callback=?",
success:function(msg){
alert("value1:" + data.value1+ " value2:" + data.value2);
}
});