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++;
        });
    }
});
4
  • 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. Commented Jun 8, 2013 at 18:57
  • @FelixKling it is a string like this : [1,2,3,4,5] I want to parse this to be an Array Commented Jun 8, 2013 at 19:19
  • Does console.log(typeof msg); show string as well? Commented Jun 8, 2013 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 Commented Jun 8, 2013 at 19:53

2 Answers 2

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]);
        }
    });
}
Sign up to request clarification or add additional context in comments.

4 Comments

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
Did you also fix the bad header in PHP noticed by Marcelo ? It looks like your browser can't detect it's JSON.
To be sure, set also the dataType argument.
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.
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",,

2 Comments

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.
@FelixKling You're right, it's not redundant if it sends a JSON. Which is not the case. I'll edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.