limit limit - 16 days ago 5
Javascript Question

jQuery ajax parameter value pulled dynamically

Wondering if there is someway to do this without using a timeout.

Right now I have an ajax request that pulls in a token via an external js. I don't have full control over this script as its provided by a 3rd party.

So it basically does an ajax request and passes me back a token vaule. I then take that value and update a form input with it.

My problem is the form submits before it has time to fully get value, hence the value is never passed.

I have some responses to work with that this 3rd party script provides, right now I am doing something like.

resonseData is passed back to me from this script..

if(responseData.dataValue !='') {
$('[name=payment_token]').val(responseData.dataValue, function(){
$("#userPaymentUpdate").submit();

});
}


^ The problem is the form submits before it has time to update the $('[name=payment_token]').val()

Is there anyway way round this aside for putting a timeout in? I thought by adding a callback like above would solve it, but apparently it doesn't.

I also have event.preventDefault(); on the form click handler, but when thats enable the 3rd party script wont execute at all. So basically need to only submit the form if that payment_token value has been updated.

Answer

If I'm reading this correctly, it looks more like an issue of order. responseData.dataValue has the value, otherwise that if condition wouldn't have processed.

Your code should look something like this:

if(responseData.dataValue !='') {
        $('input[name=payment_token]').val(responseData.dataValue); /* I'm guessing you're using a hidden field for the payment token. */
        $("#userPaymentUpdate").submit(); /* at this point, the value will have already been assigned. */
    }