本文介绍了无法获取使用Jquery.getJSON与MVC3响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用jQuery 1.5.1与MVC3一个JSON响应和JavaScript是默默地崩溃了。调试服务器端code我绝对传递一个填充的列表来响应。

在问候评论一些进一步的信息。

在Firebug返回的响应是这样的:

  [{LocationId:ASXX0413,LOCATIONNAME:阿尔巴尼,澳大利亚}]

和萤火虫也认识到它作为一个JSON对象。

我的javascript:

  weatherEvents:功能(){        jQuery的(A#getweather)。点击(函数(事件){            。事件preventDefault;
            变种查询= jQuery的(#Farm_Weather)VAL();            如果(查询===){
                返回;
            }            jQuery.getJSON(/农场/天气,{位置:查询},功能(数据){
                变种项= [];                jQuery.each(数据,功能(键,VAL){
                    items.push(<立GT;+ val.LocationId +:+ val.LocationName +< /李>);
                });                jQuery的(< UL />中,{
                    阶级:天气位置列表
                    HTML:items.join()
                })appendTo(div.weatherdiv);
            });
        });
    }

我的服务器端code:

  [HTTPGET]
    公共JsonResult天气(字符串位置)
    {
        字符串requestUrl =的String.Format(
            http://xoap.weather.com/search/search?where={0},
            HttpUtility.UrlEn code(location.Trim()));        XmlDocument的xmlDoc中=新的XmlDocument();
        XmlNodeList中节点列表= NULL;        //将到Weather.com一个电话,搜索此位置
        xmlDoc.Load(requestUrl);
        节点列表= xmlDoc.SelectNodes(/搜索/ LOC);        //把我们的节点列表,并得到一个新的匿名类型。
        VAR jsonWeather = nodeList.Cast<&XmlElement的GT;()
                                  。选择(X =>新建
                                  {
                                      LocationId = x.GetAttribute(ID),
                                      LOCATIONNAME = x.InnerText
                                  });        返回JSON(jsonWeather.ToList(),JsonRequestBehavior.AllowGet);
    }


解决方案

答案事实证明它很简单......

有一个已知的错误使用jQuery 1.5.1和附带MVC的jquery.validate插件。

进一步信息可以在这里找到和更新的插件可以在这里找到

I'm trying to get a jSON response using jQuery 1.5.1 with mvc3 and the javascript is silently crashing out. Debugging the server side code i'm definitely passing a populated list to the response.

Some further information in regards to the comments.

The response returned in firebug is thus:

[{"LocationId":"ASXX0413","LocationName":"Albany, Australia"}]

and firebug also recognises it as a jSON object.

My Javascript:

 weatherEvents: function () {

        jQuery("a#getweather").click(function (event) {

            event.preventDefault;
            var query = jQuery("#Farm_Weather").val();

            if (query === "") {
                return;
            }

            jQuery.getJSON("/Farm/Weather", { location: query }, function (data) {
                var items = [];

                jQuery.each(data, function (key, val) {
                    items.push("<li>" + val.LocationId + " : " + val.LocationName + "</li>");
                });

                jQuery("<ul/>", {
                    "class": "weather-location-list",
                    html: items.join("")
                }).appendTo("div.weatherdiv");
            });
        });
    }

My server side code:

    [HttpGet]
    public JsonResult Weather(string location)
    {
        string requestUrl = string.Format(
            "http://xoap.weather.com/search/search?where={0}",
            HttpUtility.UrlEncode(location.Trim()));

        XmlDocument xmlDoc = new XmlDocument();
        XmlNodeList nodeList = null;

        // Place a call to Weather.com and search for this location
        xmlDoc.Load(requestUrl);
        nodeList = xmlDoc.SelectNodes("/search/loc");

        // Cast our nodelist and get a new anonymous type.
        var jsonWeather = nodeList.Cast<XmlElement>()
                                  .Select(x => new
                                  {
                                      LocationId = x.GetAttribute("id"),
                                      LocationName = x.InnerText
                                  });

        return Json(jsonWeather.ToList(), JsonRequestBehavior.AllowGet);
    }

The answer it turns out it quite simple.....

There is a known bug with jQuery 1.5.1 and the jquery.validate plugin shipped with mvc.

further info can be found here and the updated plugin can be found here.

这篇关于无法获取使用Jquery.getJSON与MVC3响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:02