1

I want to get a string from a php script, and parse it to a javascript array. but got the error : SyntaxError: JSON.parse: unexpected character for the line :JSON.parse(msg);

I searched a lot , couldn't figure out where is my problem, please help check for me. Thanks.

PHP side :

header("application/json; charset=utf-8");
$sum = array(1,2,3,4,5);
echo json_encode($sum);

Javascript :

$.ajax({
    type: "POST",
    url: "save.php",
    contentType: "application/json; charset=utf-8",
    data: price,
    success: function (msg) {
        var i = 0;
        console.log(msg);
        var sum = new Array();
        sum = JSON.parse(msg);
        $('input.hj').each(function () {
            if (sum[i] > 0) {
                $(this).val(sum[i]);
            }
            i++;
        });
    }
});
Lion
  • 18,729
  • 22
  • 80
  • 110
Frank
  • 1,315
  • 7
  • 24
  • 43
  • 2
    Just have a look at the output of `console.log(msg);`. Is it an object or a string? If it is an object, you don't have to and cannot parse it. – Felix Kling Jun 08 '13 at 18:57
  • @FelixKling it is a string like this : [1,2,3,4,5] I want to parse this to be an Array – Frank Jun 08 '13 at 19:19
  • Does `console.log(typeof msg);` show `string` as well? – Felix Kling Jun 08 '13 at 19:21
  • @FelixKling if I put header("Content-type: application/json; charset=utf-8"); in php, the success function doesn't work. If I remove that header, the msg type is string – Frank Jun 08 '13 at 19:53

2 Answers2

4

Don't parse it : $.ajax parsed it for you. Just use the argument which is given to your success callback, this is the parsed array.

If your browser can't detect it's JSON, add the dataType argument.

Note also that you don't have to manage the i counter yourself : one is passed by each :

dataType: 'json',
success: function(sum){
    $('input.hj').each(function(i){
        if (sum[i] > 0) {
            $(this).val(sum[i]);
        }
    });
}
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • without parsing, I got the value as a String like this "[1,2,3,4,5]" it output "[" "1" "," ..... It's not what I want. Thanks – Frank Jun 08 '13 at 19:23
  • 1
    Did you also fix the bad header in PHP noticed by Marcelo ? It looks like your browser can't detect it's JSON. – Denys Séguret Jun 08 '13 at 19:24
  • To be sure, set also the dataType argument. – Denys Séguret Jun 08 '13 at 19:26
  • I fixed the header like this header("Content-type: application/json; charset=utf-8"); then the success function doesn't work anymore, no console output. – Frank Jun 08 '13 at 19:56
0

There's no need to parse it. Also, in your PHP script, your header should be:

header("Content-type: application/json; charset=utf-8");

In your JS, if price is a string (or float or int), there is no need to send it as json. For simplification, you may want to remove contentType: "application/json; charset=utf-8",,

Marcelo Pascual
  • 810
  • 8
  • 20
  • 3
    It's not redundant. The `contentType` option specifies the format of the data **sent** to the sever. Of course if `price` is not JSON, then the header is wrong, but that's a different reason. – Felix Kling Jun 08 '13 at 18:59
  • @FelixKling You're right, it's not redundant if it sends a JSON. Which is not the case. I'll edit. – Marcelo Pascual Jun 08 '13 at 19:03