MVC JSON 方法向 JQuery 返回无效 JSON?
我在 Jquery 解析我发回的 JSON 时遇到问题...但是,这很奇怪,因为我使用的是 MVC 的 JSON 方法。
这是我的设置。我有一个非常简单的函数:
$.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);
}
});
它总是运行错误函数,显示:
parsererror: [{"Value":"Toyota","ID":160}]
我无法弄清楚为什么它会这样做...... 它正在使用旧版本的 JQuery - 我读到 JQuery JSON 解析器现在更加严格 - 但我无法弄清楚我的 JSON 出了什么问题。
即使它是错误的,这也是非常令人沮丧的,因为我正在使用 MVC Json 函数来生成这个:
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);
}
我一定错过了一些明显的东西......仍然掌握窍门JQuery/MVC 但这些东西确实减慢了我的进度。
果然,JQuery结果如下(根据Chrome的开发者工具栏)
[{"Value":"Toyota","ID":160}]
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}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 jQuery AJAX 调用中的数据类型更改为“text json”。我怀疑响应内容类型标头可能存在问题,或者导致 jQuery 不将 dataType 确认为 json 的其他问题。使用“text json”将导致 jQuery 在将其转换为 js 对象之前将其接受为纯文本。
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.
我刚刚尝试了上面的方法,它解析得很好,但请记住它已将其作为数组中的单个记录返回(由于从 C# 返回 IEnumerable)。
I've just tried the above and it parses it fine, however remember it has returned it as a single record in an array (due to returning an IEnumerable from the C#).