Tarik Mokafih
Tarik Mokafih

Reputation: 1257

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

what is the error here :

.done(function(data) {
                var json = JSON.parse( data );
                if(json['status'] === "success"){
                    //some processing
                }
                else {
                    alert( "error 2" );
                }
            })

I got the error message on: var json JSON.parse('('+data+')')

data is returned from a php script :

/*...............*/
$sql->execute();
$i = 0;
while($result = $sql->fetch(PDO::FETCH_ASSOC){
    $response["affiliates"][i]["affiliate_name"] = $result["coupon_name"];
    $response["affiliates"][i]["affiliate_id"] = $result["coupon_id"];
    $i++;
}
$response["status"] = "success";
echo json_encode($response); 

Upvotes: 0

Views: 30453

Answers (3)

bestalign
bestalign

Reputation: 227

JSON format only use curly and squared braces. You shouldn't append parentheses.

Upvotes: 1

Michael Lorton
Michael Lorton

Reputation: 44376

Because it's wrong.

"(1)" (for example) is not a valid JSON string. Why are you pasting those parens on at all?

Upvotes: 2

Stephen
Stephen

Reputation: 5450

Look at the spec for JSON (easily understood version here: http://json.org/). There is nowhere that says that parenthesis are valid. ({"foo": true}), for example will never parse. It may be evaled as it is valid javascript, but javascript is not JSON.

Upvotes: 4

Related Questions