1

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}]
asked Apr 28, 2011 at 20:51
6
  • Can you show the exact JSON that is being generated? Commented Apr 28, 2011 at 20:52
  • @Pekka- it is as the error message shows, but I went ahead and added it for clarity. Commented Apr 28, 2011 at 20:54
  • Try switching dataType: 'json' to type: 'text/json'. Commented Apr 28, 2011 at 20:55
  • @Tejs: that didn't work but a college suggested "text json" (without the slash) right before you commented and that worked like a charm... I feel like an idiot but also irked by how picky this is... maybe I missed it but it wasn't easy to catch from the documentation. Commented Apr 28, 2011 at 21:00
  • Odd, it shouldn't work, considering text json isnt a valid content type. However, don't fix what isnt broken! Commented Apr 28, 2011 at 21:02

2 Answers 2

2

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.

answered Apr 28, 2011 at 21:02
Sign up to request clarification or add additional context in comments.

Comments

0
var parsed = jQuery.parseJSON('[{"Value":"Toyota","ID":160}]');

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#).

answered Apr 28, 2011 at 21:15

1 Comment

Yeup, it wasn't that the data was invalid- (apparently) it's the way Chrome and IE handle it and how they need the the wrong ContentType. See comments here: api.jquery.com/jQuery.parseJSON (this was the exact same situation as Technoangel 's.)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.