简体   繁体   中英

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); 

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 eval ed as it is valid javascript, but javascript is not JSON.

Because it's wrong.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM