js跨域访问

客户端 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 }

 

 

js跨域访问,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。