Techy Techy - 2 months ago 7
Ajax Question

How to get a particular field value from json response

I am trying to call an external API system from my website.In the ajax response,I get the following data.

object(stdClass)#3 (3) {


["status"]=>
string(7) "success"
["code"]=>
int(200)
["data"]=>
object(stdClass)#4 (3) {

["desktop_url"]=>
string(113) "http://landing.beta.learning.social/remote?token=a_token_value"

["mobile_url"]=>
string(115) "http://m.landing.beta.learning.social/remote?token=a_token_value"

}
}


I need to get the value of the
desktop_url
field from this response.So that I can redirect page with the url from that field.I have tried with the following code.

$.ajax({
type: "POST",
url: path,
data: "email=" + $("#email_data").val() + "&fname=" + $("#fname_data").val()+"&grade="+$("#gradeID_data").val()+
"&lastname="+$("#lname_data").val(),
success: function(message){
$(message).map(function(item){console.log (item.data);});
}
});


This is not working for me and giving me the following error

Error: Syntax error, unrecognized expression: object(stdClass)#3 (3...

Answer

You need to json encode the response from the api call

<?php 
$response = $api->call($params[,...]);


header('content-type:application/json');
exit(json_encode($response));
Comments