tags:

views:

245

answers:

3

I have tried upgrading from jQuery 1.2.6 to 1.4.1. A JSON query is now failing with error : "parseerror, invalid label"

I have simplified the code to bare minimum.

var jsonUrl = "process_01.php";
$.ajax({
 type: "POST",
 url:  jsonUrl,   
 data: "var=myVar",        
 dataType: "json",    
 beforeSend: function(msg) {
 },
 success: function(msg) {
 },
 error: function (msg, textStatus, errorThrown) {
   alert("an error has occured (" + textStatus + " " + errorThrown +")" );
   }
});
return false;
}

Output of php program is {"foo":"bar","IsTrue":"true"}

I can see this in firebug

A: 

There should be single quotes around the values sent. Like this guy did.

Alfabravo
but he already has quotes around his values
Vinodh Ramasubramanian
tried with both single echo json_encode($returnArray);Can someone else try this?
harry_T
I found http://dev.jqueryui.com/ticket/5104 which seems to say that json_encode isn't "valid" enough. Instead they implemented a custom function `array_to_json` as seen in http://dev.jqueryui.com/browser/trunk/demos/autocomplete/search.php?rev=3749#L636. I still can't figure out exactly what makes it invalid though.
matschaffer
@matschaffer: That should be it.
Alfabravo
A: 

Make sure your content type is set to application/json.

Also look at this, looks similar to your problem.

Vinodh Ramasubramanian
It looks like my php script was throwing out a blank line before the header.
harry_T
A: 

If it helps anyone else who stumbles across this, the original poster said he wasn't sending out the header as application/json. In my case I ran into trouble parsing quoted apostrophes inside a double quoted string like this:

{"description": "An invalid person\'s JSON"}

Looks like 1.3.2 took this, but 1.4.1 rejects it. Guess I'll have to find a more graceful approach to escaping on the server side. See http://jsbin.com/oveci3 for what I used for reference.

matschaffer