客户端 HTML
1 <div id="oid"></div> 2 <script type="text/javascript"> 3 //获取货号 4 $.ajax({ 5 6 url: "http://192.168.1.191/H.ashx", 7 type: "GET", 8 dataType: ‘jsonp‘, 9 //jsonp的值自定义,如果使用jsoncallback,那么服务器端,要返回一个jsoncallback的值对应的对象. 10 jsonp: ‘jsoncallback‘, 11 //要传递的参数,没有传参时,也一定要写上 12 data: null, 13 timeout: 5000, 14 //返回Json类型 15 contentType: "application/json;utf-8", 16 //服务器段返回的对象包含name,openid. 17 success: function (result) { 18 19 document.getElementById(‘oid‘).innerText=result.name+":"+result.openid; 20 }, 21 error: function (jqXHR, textStatus, errorThrown) { 22 alert(textStatus); 23 } 24 }); 25 26 </script>
服务端 H.ashx
1 <%@ WebHandler Language="C#" Class="H" %> 2 3 using System; 4 using System.Web; 5 6 public class H : IHttpHandler { 7 8 public void ProcessRequest (HttpContext context) { 9 context.Response.ContentType = "text/plain"; 10 11 string result = context.Request.QueryString["jsoncallback"] + "({\"name\":\"测试编号为\",\"openid\":\"123456789\"})"; 12 13 context.Response.Clear(); 14 context.Response.Write(result); 15 context.Response.End(); 16 17 18 } 19 20 public bool IsReusable { 21 get { 22 return false; 23 } 24 } 25 26 }
原文:http://www.cnblogs.com/yingshiyu/p/3736867.html