0

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); 
2
  • 1
    It's probable data isn't some JSON... Commented Jul 16, 2014 at 20:45
  • 1
    Why are you adding () around it? not {}? Commented Jul 16, 2014 at 20:46

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I still get : SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
Then try console.logging out the data. See what it looks like. Does the data match the spec?
data is a php array encoded to json using json_encode($myarray)
That's obviously not all it is, otherwise you wouldn't be getting the error. I can read the code you wrote. What is the server response. The FULL server response. And if you're using jQuery, are you sure jQuery isn't autoconverting the data for you.
2

Because it's wrong.

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

2 Comments

I took them off but stii getting : SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
Well, at least you've advanced a bit. Open the URL directly in your browser and see what it looks like.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.