问题描述
已经越来越从jQuery的一个Ajax请求一个parsererror,我试图改变在POST到GET,在几个不同的方式返回数据(创建类等),但我似乎无法找出什么问题是,
Been getting a "parsererror" from jquery for an Ajax request, I have tried changing the POST to a GET, returning the data in a few different ways (creating classes, etc.) but I cant seem to figure out what the problem is.
我的项目是在MVC3和我使用jQuery 1.5
我有一个下拉列表,并在onchange事件我火了一个电话的基础上选择了哪一得到一些数据。
My project is in MVC3 and I'm using jQuery 1.5I have a Dropdown and on the onchange event I fire off a call to get some data based on what was selected.
下拉列表(此加载从Viewbag列表中的意见,并触发该事件正常工作)
Dropdown: (this loads the "Views" from the list in the Viewbag and firing the event works fine)
@{
var viewHtmls = new Dictionary<string, object>();
viewHtmls.Add("data-bind", "value: ViewID");
viewHtmls.Add("onchange", "javascript:PageModel.LoadViewContentNames()");
}
@Html.DropDownList("view", (List<SelectListItem>)ViewBag.Views, viewHtmls)
使用Javascript:
Javascript:
this.LoadViewContentNames = function () {
$.ajax({
url: '/Admin/Ajax/GetViewContentNames',
type: 'POST',
dataType: 'json',
data: { viewID: $("#view").val() },
success: function (data) {
alert(data);
},
error: function (data) {
debugger;
alert("Error");
}
});
};
以上code调用成功MVC方法返回:
The above code successfully calls the MVC method and returns:
[{"ViewContentID":1,"Name":"TopContent","Note":"Content on the top"},
{"ViewContentID":2,"Name":"BottomContent","Note":"Content on the bottom"}]
但jQuery的火灾为$。阿贾克斯()方法错误事件说:parsererror。
But jquery fires the error event for the $.ajax() method saying "parsererror".
推荐答案
最近,我遇到了这个问题,并偶然发现了这个问题。
I recently encountered this problem and stumbled upon this question.
我用更简单的方法解决它。
I resolved it with a much easier way.
方法一
您可以删除数据类型:'JSON'
从对象属性的文字...
You can either remove the dataType: 'json'
property from the object literal...
方法二
或者,你可以做什么@Sagiv被返回的数据作为的Json
说。
Or you can do what @Sagiv was saying by returning your data as Json
.
为什么会出现此 parserror
消息的原因是,当你简单地返回一个字符串或其他值,它是不是真的的Json $ C解析时,$ C>,所以解析器会失败。
The reason why this parserror
message occurs is that when you simply return a string or another value, it is not really Json
, so the parser fails when parsing it.
所以,如果你删除数据类型:JSON
属性,也不会尝试分析它作为的Json
。
So if you remove the dataType: json
property, it will not try to parse it as Json
.
使用另一种方法,如果你一定要回你的数据作为的Json
,解析器会知道如何正确处理它。
With the other method if you make sure to return your data as Json
, the parser will know how to handle it properly.
这篇关于jQuery的返回&QUOT; parsererror&QUOT;为ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!