标签:baidu har 程序 处理程序 ons ashx oid false 定义
一个一般处理程序服务端
返回的数据是json
public void ProcessRequest(HttpContext context) { citys c = new citys(); c.age = "12"; c.city = "bj"; c.hosst = "www.baidu.com"; c.name = "张三"; c.weather = "天气";
string cc = JsonConvert.SerializeObject(c); context.Response.ContentType = "text/plain"; context.Response.Write( cc ); }
这个一般处理程序返回jsonp
public void ProcessRequest(HttpContext context) { citys c = new citys(); c.age = "12"; c.city = "bj"; c.hosst = "www.baidu.com"; c.name = "张三"; c.weather = "天气"; string cc = JsonConvert.SerializeObject(c); context.Response.ContentType = "text/plain"; string callbackFunName = context.Request["callback"]; context.Response.Write(callbackFunName + "([" + cc + "])"); }
可以看出jsonp是包裹着json这样理解
json:cc
jsonp:callbackFunName(cc)
下面是客户端的请求
$("#aaa").click(function () { $.ajax({ type: "get", async: false, url: "http://www.server.com/handler1.ashx", dataType: "jsonp", jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback) jsonpCallback: "success_jsonpCallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名 success: function (data) { alert(data); alert(data[0].name); }, error: function () { alert(‘fail‘); } }); });
标签:baidu har 程序 处理程序 ons ashx oid false 定义
原文地址:https://www.cnblogs.com/dcrBook/p/10037075.html