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 }

 

 

转载于:https://www.cnblogs.com/yingshiyu/p/3736867.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值