-1

I've got a AJAX function running which returns a response and I need to get the values from the responseText

jQuery.ajax({
                url: process_payment.ajaxurl,
                type: 'post',
                dataType: 'json',
                data: {
                    action: 'process_payment',
                    payment_method_id: result.paymentMethod.id
                },
                complete: function(json) {
                    var response = json.responseText;
                    console.log(response);
                    handleServerResponse(json);
                }
            }) 

This is the contents of the variable response

{"requires_action":true,"payment_intent_client_secret":"0eo9ei48494404014044"}0

How do i the value of 'requires_action' for example, i thought i could do response.requires_action but this returns undefined.

EDIT

This is also not working:

 jQuery.post(process_payment.ajaxurl, {
                action: 'process_payment',
                payment_method_id: result.paymentMethod.id
            }, 'json').done(handleServerResponse)

function handleServerResponse(response) {
    console.log(response);
}
Shaun
  • 757
  • 1
  • 8
  • 36
  • Rename `complete` to `success` and use `json.requires_action` – Phil Dec 12 '19 at 12:59
  • use as `json.requires_action`, thle line `var response = json.responseText;` is not needed – Devsi Odedra Dec 12 '19 at 12:59
  • Do i have to use success for this to work? – Shaun Dec 12 '19 at 13:02
  • No, you could also use the `.done()` or `.then()` methods, eg `$.post(process_payment.ajaxurl, { action: 'process_payment', payment_method_id: result.paymentMethod.id }, 'json').done(handleServerResponse)` – Phil Dec 12 '19 at 13:03
  • 1
    https://stackoverflow.com/questions/5240876/difference-between-success-and-complete – jvk Dec 12 '19 at 13:03
  • @Phil How do i then access the json object in handleServerResponse? – Shaun Dec 12 '19 at 13:11
  • Eh? It's the first argument, eg `function handleServerResponse(obj) { console.log(obj.requires_action) }` – Phil Dec 12 '19 at 13:13
  • Hi Phil, sorry i may be missing the obvious here but i've just updated my question with what you're suggesting and it's not working. Forgive me, I'm not very experienced in javascript – Shaun Dec 12 '19 at 13:17

1 Answers1

1

You need to use success callback instead of complete since complete keeps xhr object as a parameter in the callback or parse the response text to convert JSON string to object.

In case of success it will be parsed by default since you specified dataType as json and keep an error callback if necessary to handle the error case.

jQuery.ajax({
    url: process_payment.ajaxurl,
    method: 'post',
    // if you're using versions of jQuery prior to 1.9.0 - use `type: 'post'`,
    dataType: 'json',
    data: {
        action: 'process_payment',
        payment_method_id: result.paymentMethod.id
    },
    success: function(response) {
        console.log(response);
        handleServerResponse(response);
    }
}) 

Refer : https://api.jquery.com/jQuery.ajax/

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188