0

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.

1
  • Show the full context of this code. We don't know how it fits into the form processing or where you are calling the ajax from Commented Nov 20, 2016 at 5:04

1 Answer 1

1

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. */
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.. how I missed that. Was thinking I needed a callback for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.