eXtreme eXtreme - 8 months ago 56
JSON Question

Parsing JSON encoded PHP array with Javascript - unexpected character

I'm using PHP to encode array to JSON string like this:

$price['price'] = "20";
$price = json_encode($price)
return $price;

so when I'm accessing the script I get this data:


Here is my jQuery/Javascript code:

var frm = $(document.form);
var data = JSON.stringify(frm.serializeArray());

method: "POST",
url: "script.php",
data: {json : data },
dataType: 'json',
cache: false,
success: function(json)
var obj = JSON.parse(json);

The error I get in Firefox:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data


The whole point of specifying dataType: 'json' is that jQuery will parse the results for you. Your json varaible already contains a JavaScript object. You don't need to parse it twice.

You also shouldn't call it "json", since it's not JSON at that point:

success: function(data) {