undefined value from ajax success response using json data

Viewed 51

My CURL code is as follows:

$ch = curl_init("http://my ip address/api/FeedbackCustomerDetails?accessKey=$access_key&AccountNo=$subscriber_code");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);                                                                                                                  
$result = curl_exec($ch);
$response = print stripslashes(json_encode($result));

The response from my curl is as follows:

"{"CENTER":"BRADFORD","STATUS":"0","SUB_NAME":"PETER ","ACCOUNT_NO":"HB92008117","ASSOCIATE_ID":"850N027","STATUS_TEXT":"SUCCESS","REGION":"UK"}"

Without strip_slashes the output is:

"{\"CENTER\":\"BRADFORD\",\"STATUS\":\"0\",\"SUB_NAME\":\"PETER \",\"ACCOUNT_NO\":\"HB92008117\",\"ASSOCIATE_ID\":\"850N027\",\"STATUS_TEXT\":\"SUCCESS\",\"REGION\":\"UK\"}"

I tried the following and always get "undefined"

    success: function(response) {
        alert(response.ACCOUNT_NO);
},

I need to display the data in a span id. I tried the following too:

$("#subscriber code").text(response[0].ACCOUNT_NO);

response.ACCOUNT_NO; always results in UNDEFINED.

What am i doing wrong?? Requesting help..

1 Answers

Have you tried to JSON.parse() the result first? You have an "undefined" value of this property, because your response is a string, not an object. And string do not have property named "ACCOUNT_NO". You should parse your response into object first.

response = JSON.parse(response);
alert( response.ACCOUNT_NO );
Related