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] => johndoe@gmail.com
    [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) {
                }
            })
Saad Masood
  • 312
  • 1
  • 2
  • 16
  • You should encode the array as JSON and send that JSON to the client. – Felix Kling Apr 02 '21 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":"john@gmail.com","merchantId":"10"} @FelixKling – Saad Masood Apr 02 '21 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. – Felix Kling Apr 03 '21 at 06:25

1 Answers1

1

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

code-a1
  • 71
  • 1
  • 5
  • 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":"john@gmail.com","merchantId":"10"} – Saad Masood Apr 02 '21 at 21:25
  • @SaadMasood fix this: console.log(data.responseCode); – code-a1 Apr 02 '21 at 21:36
  • Return undefined by this console.log(data.responseCode) – Saad Masood Apr 02 '21 at 21:40
  • 1
    @SaadMasood you must also insert dataType: "json", – code-a1 Apr 02 '21 at 21:44