jQuery returning "parsererror" for ajax request
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.
My project is in MVC3 and I'm using jQuery 1.5 I have a Dropdown and on the onchange event I fire off a call to get some data based on what was selected.
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:
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");
}
});
};
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"}]
But jquery fires the error event for the $.ajax() method saying "parsererror".
Anwsers
- sorry, should have been more specific, it fires the $.ajax() error function { alert("Error"); }– dkarzonFeb 21, 2011 at 1:08
- Any chance of a live link? Do you see the JSON data you show in Firebug?– PekkaFeb 21, 2011 at 1:12
- No I dont have a live link. But yes that is the JSON response shown in Firebug.– dkarzonFeb 21, 2011 at 1:17
- @mu is too short - to be perfectly honest, 'javascript:' shouldn't be in there at all...– JAAuldeFeb 21, 2011 at 4:34
- Probably not, was just tried a few things. But thats not the issue im having, the event is being fired correctly.– dkarzonFeb 21, 2011 at 6:53
- I'm also getting this issue with jQuery 1.7.2. Mine is a bit more complex though, as I'm attempting to use a ajaxPrefilter against a CORS resource.
- I suspect I'm having the same issue as this guy: bugs.jquery.com/ticket/12783 I'm only posting this here in case someone else is also getting this issue on the latest version of jquery while trying to parse valid JSON. Jan 2, 2013 at 9:47
I recently encountered this problem and stumbled upon this question.
I resolved it with a much easier way.
Method One
You can either remove the dataType: 'json'
property from the object literal...
Method Two
Or you can do what @Sagiv was saying by returning your data as Json
.
The reason why this parsererror
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.
So if you remove the dataType: json
property, it will not try to parse it as Json
.
With the other method if you make sure to return your data as Json
, the parser will know how to handle it properly.
0 Comments
If you have any doubts, Please let me know