js跨域访问示例(客户端/服务端)_技术学院_宜昌市隼壹珍商贸有限公司

您好,欢迎访问宜昌市隼壹珍商贸有限公司

400 890 5375
当前位置: 主页 > 新闻动态 > 技术学院

js跨域访问示例(客户端/服务端)

发布时间:2026-01-17  |  点击率:
复制代码 代码如下:
<div id="oid"></div>
<script type="text/javascript">
//获取货号
$.ajax({

url: "http://192.168.1.191/H.ashx",
type: "GET",
dataType: 'jsonp',
//jsonp的值自定义,如果使用jsoncallback,那么服务器端,要返回一个jsoncallback的值对应的对象.
jsonp: 'jsoncallback',
//要传递的参数,没有传参时,也一定要写上
data: null,
timeout: 5000,
//返回Json类型
contentType: "application/json;utf-8",
//服务器段返回的对象包含name,openid.
success: function (result) {

document.getElementById('oid').innerText=result.name+":"+result.openid;
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});

</script>

服务端 H.ashx
复制代码 代码如下:
<%@ WebHandler Language="C#" Class="H" %>

using System;
using System.Web;

public class H : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";

string result = context.Request.QueryString["jsoncallback"] + "({\"name\":\"测试编号为\",\"openid\":\"123456789\"})";

context.Response.Clear();
context.Response.Write(result);
context.Response.End();


}

public bool IsReusable {
get {
return false;
}
}

}

全国统一服务电话

400 890 5375

电子邮箱:879577@qq.com

公司地址:宜昌市西陵区黄河路5号三峡明珠10栋1051室

咨询微信

TEL:13680874598