当前位置:编程学习 > C#/ASP.NET >>

mvc4环境下使用ajax实现跨域请求

在前台js中使用

        var arrLNG = new Array();
        var arrLAT = new Array();
        $.ajax({
            type: "post",
            url: "(ip)/Home/deviceInfo",
            async: false,
            dataType: "jsonp",
            jsonp: 'callback',
            success: function (data) {
                 //取数据
                for (var i = 0; i < data.length; i++) {
                    arrLNG[i] = data[i].LNG;
                    arrLAT[i] = data[i].LAT;
                }
            }
        });

controller中的代码


    public class JsonpResult : JsonResult
    {
        private static readonly string JsonpCallbackName = "callback";
        private static readonly string CallbackApplicationType = "application/json";

        
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if ((JsonRequestBehavior == JsonRequestBehavior.DenyGet) &&
                  String.Equals(context.HttpContext.Request.HttpMethod, "GET"))
            {
                throw new InvalidOperationException();
            }
            var response = context.HttpContext.Response;
            if (!String.IsNullOrEmpty(ContentType))
                response.ContentType = ContentType;
            else
                response.ContentType = CallbackApplicationType;
            if (ContentEncoding != null)
                response.ContentEncoding = this.ContentEncoding;
            if (Data != null)
            {
                String buffer;
                var request = context.HttpContext.Request;
                var serializer = new JavaScriptSerializer();
                if (request[JsonpCallbackName] != null)
                    buffer = String.Format("{0}({1})", request[JsonpCallbackName], serializer.Serialize(Data));
                else
                    buffer = serializer.Serialize(Data);
                response.Write(buffer);
            }
        }
    }
    public static class ContollerExtensions
    {
        
        public static JsonpResult Jsonp(this Controller controller, object data)
        {
            JsonpResult result = new JsonpResult()
            {
                Data = data,
                
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

            return result;
        }
    }


    public class HomeController : Controller
    {
        DataClasses1DataContext ddc = new DataClasses1DataContext();

        public JsonpResult deviceInfo()
        {
           // 查询数据
            var DeviceInfo = from device in ddc.YS_tbl_SHB
                             select new
                             {
                                 LNG = device.LNG,
                                 LAT = device.LAT
                             };

            var li = DeviceInfo.ToList();


            return this.Jsonp(li);//调用jsonp方法
        }
}


  这个ajax请求是在网上看到的例子,我写了一个demo,但是ajax请求无法跳转到后台,向各位大大求解,刚开始学ajax。
  firebug中显示的是  
  在调用Jsonp这个方法时能传如上述代码中的数据li么? mvc4 ajax jsonp --------------------编程问答-------------------- 没人回答啊~~~~~ --------------------编程问答-------------------- 之前我也弄跨域请求来的,但是没找到失败的原因,无奈之下只能用模拟请求来解决问题 --------------------编程问答--------------------
引用 2 楼 id270 的回复:
之前我也弄跨域请求来的,但是没找到失败的原因,无奈之下只能用模拟请求来解决问题

能介绍下你的方法么。纠结了半天弄不出来 --------------------编程问答-------------------- 感觉你的ajax 跨域请求没写对 你在查查 --------------------编程问答--------------------
引用 4 楼 sibiyellow 的回复:
感觉你的ajax 跨域请求没写对 你在查查

如果是本地的ajax请求是可以实现的了,也就是说,直接访问本机的action是可以返回结果的,当通过ip就无法请求。应该是controller写的不对 --------------------编程问答-------------------- firebug 的那个500错误 你点开后能查看到什么? --------------------编程问答--------------------
引用 6 楼 sibiyellow 的回复:
firebug 的那个500错误 你点开后能查看到什么?




--------------------编程问答-------------------- 这个应用我是发布在iis7上的 --------------------编程问答-------------------- 额 这样也看不出来具体的错误 你先把这个请求的这个方法 写上最简单的返回值 看能不能跨域请求到值,如果可以的话 那么你的ajax就写对 就是你的controller写错了.
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,