MVC JSON方法返回无效JSON jQuery的? [英] MVC JSON method returning invalid JSON to JQuery?

查看:135
本文介绍了MVC JSON方法返回无效JSON jQuery的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有jQuery的解析,我发回JSON问题......然而,这是很奇怪,因为我使用MVC的JSON的方法。

下面是我的设置。我有一个很简单的功能:

  $。阿贾克斯({
    网址:URLd,
    数据类型:JSON,
    数据:{年份:$('#VehicleYear)VAL(),值:request.term},
    成功:功能(数据,textStatus,jqXHR){警报(成功!); },
    错误:功能(XMLHtt prequest,textStatus){
        警报(textStatus +:+ XMLHtt prequest.responseText);
    }
});

它总是运行误差函数表示:

parsererror:[{值:丰田,标识:160}]

我想不通为什么在世界上它是这样做的...... 它与一个旧版本的JQuery 的工作 - 我读了JQuery的JSON解析器是更加严格了不少,现在 - 但我想不出这有什么错我的JSON。

即使是错的,这是非常令人沮丧,因为我使用的 MVC的的Json功能的生成这样的:

 公众的ActionResult GetVehicleModels(INT年,INT MakeID,字符串值= NULL)
{
    VAR modlMatchs =(从VMYX在ent.VehicleMakeYearXREFs
                        加入VM在ent.VehicleModels
                        在VMYX.VehicleModelID等于VM.VehicleModelID
                        参加VMA在ent.VehicleMakes
                        在VM.VehicleMakeID等于VMa.VehicleMakeID
                        其中,VMYX.ModelYear ==年和放大器;&安培; VMa.VehicleMakeID == MakeID&放大器;&安培; VM.VehicleModelName.StartsWith(价值)
                        排序依据VMa.VehicleMakeName
                        选择新{值= VM.VehicleModelName,ID = VM.VehicleModelID})是不同的()取(10).ToList()。    返回this.Json(modlMatchs,应用/ JSON,JsonRequestBehavior.AllowGet);
}

我必须失去了一些东西昭然若揭......仍然得到的JQuery / MVC的窍门,但这些东西真的放缓我的进步。

果然,JQuery的结果如下所示(据Chrome的开发者工具栏)

  [{值:丰田,ID:160}]


解决方案

更改您的dataType在jQuery的AJAX调用文本JSON。我怀疑有可能是在响应content-type标题,还是其他什么东西是造成jQuery的承认不承认具体的数据类型为JSON的一个问题。使用文字JSON将导致其转换为一个JS对象之前jQuery来接受它作为明文。

I am having problems with Jquery parsing the JSON I am sending back... however, this is very odd because I am using MVC's JSON method.

Here's my setup. I have a very simple function:

$.ajax({
    url: URLd,
    dataType: 'json',
    data: { Year: $('#VehicleYear').val(), Value: request.term },
    success: function (data, textStatus, jqXHR) { alert("Success!"); },
    error: function(XMLHttpRequest, textStatus) {  
        alert(textStatus + ": " + XMLHttpRequest.responseText);  
    }  
});

It always runs the error function which shows:

parsererror: [{"Value":"Toyota","ID":160}]

I cannot figure out why in the world it is doing this... it was working with an older version of JQuery - and I read that the JQuery JSON parser is quite a bit more strict now- but I can't figure out what's wrong with my JSON.

Even if it is wrong, that's very frustrating because I'm using MVC's Json function to generate this:

public ActionResult GetVehicleModels(int Year, int MakeID, string Value = null)
{
    var modlMatchs = (from VMYX in ent.VehicleMakeYearXREFs
                        join VM in ent.VehicleModels
                        on VMYX.VehicleModelID equals VM.VehicleModelID
                        join VMa in ent.VehicleMakes
                        on VM.VehicleMakeID equals VMa.VehicleMakeID
                        where VMYX.ModelYear == Year && VMa.VehicleMakeID == MakeID && VM.VehicleModelName.StartsWith(Value)
                        orderby VMa.VehicleMakeName
                        select new { Value = VM.VehicleModelName, ID = VM.VehicleModelID }).Distinct().Take(10).ToList();

    return this.Json(modlMatchs, "application/json", JsonRequestBehavior.AllowGet);
}

I must be missing something glaringly obvious... still getting the hang of JQuery/MVC but these things are really slowing my progress.

Sure enough, the JQuery result looks as follows (according to Chrome's developer toolbar)

[{"Value":"Toyota","ID":160}]

解决方案

Change your dataType in the jQuery AJAX call to "text json". I suspect there may be a problem with the response content-type header, or something else that's causing jQuery not to acknowledge the dataType as json. Using "text json" will cause jQuery to accept it as plaintext before converting it to a js object.

这篇关于MVC JSON方法返回无效JSON jQuery的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆