I am getting ajax response in array format from php url. How to extract array response values in jQuery?
FYI:
PHP array is:
$response = array('msg' => 'Hello', 'html' => '<b>Good bye</b>');
I am getting $response array in my ajax response. i.e.
var promo = "promo=45fdf4684sfd";
$.ajax({
type: "POST",
url: baseJsUrl + "/users/calc_discount",
data: promo,
success: function (msg) { // I am getting $response here as ajax response.
//alert(msg);
// Here I want to check whether response is in array format or not. if it is in array format, I want to extract msg here and want to use response array values.
}
});
Let me know answer pls.
Thanks.
You should echo that
$responsewithjson_encode().You should probably set
dataType: 'json'too inside the object literal you send to$.ajax().Then you can access it natively with JavaScript using the dot operator inside your success callback…
BTW, this line…
… isn’t valid PHP. Remove the brackets from the first key.