//参考 http://www.cnblogs.com/yuwensong/archive/2013/05/28/3103064.html
后台:
public void ProcessRequest(HttpContext context) {
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
//获取回调函数名 string callback = context.Request.QueryString["callback"]; //json数据 string json = "{"name":"chopper","sex":"man"}"; context.Response.ContentType = "application/json"; //输出:回调函数名(json数据) context.Response.Write(callback + "(" + json + ")"); }
前台:
function fnHello() { $.ajax({ url: "http://172.16.74.102:88/WebAPI/LoginHandller.ashx", dataType: "jsonp", jsonpCallback: "person", data:{aaa:"1111"},//参数后台可以得到 success: function (data) { alert(data.name + " is a a" + data.sex); } }); } //回调函数person function person(data) { alert(data.name + " is a " + data.sex); }