直接上最后的结果吧
JS:
$.ajax({
type: "GET",
async: false,
crossDomain: true,
url: "www.test.com/TestHandler.ashx",
data: { Id: "1"},
dataType: "jsonp",
jsonp: "callback",
jsonpCallback: "callbackHandler",
success: function (data) {
//success
},
error: function (x, status, error) {
//error
}
});
服务端(这里我用的是ashx一般处理程序):
public void ProcessRequest(HttpContext context)
{
JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, Content-Type,Authorization, Accept, X-Requested-With");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); var list=数据; if (string.IsNullOrEmpty(context.Request["callback"]))
{
context.Response.Write(jsonHelper.Serialize(list));
}
else
{
context.Response.Write(string.Format("{0}({1})", context.Request["callback"], jsonHelper.Serialize(list)));
}
}