0

I have a response from PHP I am trying to read this on JS but didn't get the value. I want to get the value of responseCode. But it returns me undefined.

Response Code :

Array
(
    [request] => 
    [receiptId] => 11233-555
    [responseCode] => 0
    [message] => PSI-3217:Maximum number of attempts reached.
    [transTime] => 4/2/2021 8:50:09 PM
    [merchantEmail] => [email protected]
    [merchantId] => 19
)

JS Code :

           $.ajax({
                method: "POST",
                url: "https://john.com/ca/pay.php",
                data: objData,
                success: function (data, status) {
                    
                    console.log(data['responseCode']);
                   
                },
                error: function (error) {
                }
            })
3
  • You should encode the array as JSON and send that JSON to the client. Commented Apr 2, 2021 at 21:01
  • I am unable to read the JSON. I am trying by this way JSON.parse(data.responseCode) and return error Uncaught SyntaxError: Unexpected token u in JSON at position 0 {"request":null,"receiptId":"123555","responseCode":"0","message":"PSI-3217:Maximum number of attempts reached.","transTime":"4\/2\/2021 9:21:38 PM","merchantEmail":"[email protected]","merchantId":"10"} @FelixKling Commented Apr 2, 2021 at 21:29
  • 1
    If you don't configure jQuery to decode the JSON automatically then you'd have to do JSON.parse(data).responseCode. Anyways, the duplicate shows exactly what to do. Commented Apr 3, 2021 at 6:25

1 Answer 1

1

Simply use "json_encode" in your php code and return to the request the array in json

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

4 Comments

I am unable to read the JSON. I am trying by this way JSON.parse(data.responseCode) and return error Uncaught SyntaxError: Unexpected token u in JSON at position 0 {"request":null,"receiptId":"123555","responseCode":"0","message":"PSI-3217:Maximum number of attempts reached.","transTime":"4\/2\/2021 9:21:38 PM","merchantEmail":"[email protected]","merchantId":"10"}
@SaadMasood fix this: console.log(data.responseCode);
Return undefined by this console.log(data.responseCode)
@SaadMasood you must also insert dataType: "json",

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.