有关跨域访问的的文章,可以搜到很多,基本上大同小异,下面有个不错的访问示例,大家可以参考下
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
- <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;
- }
- }
- }
来源: