23

I have the following code:

$.ajax({
      type: 'GET',
      url: 'index.php?route=checkout/onepagecheckout/getpaypaldata',
      dataType: 'json',
      success: function(json) {
                              $('#pp_info').html(json['output']);
                              $('#payment').submit();
                              }
      });

The ajax requests receives a json object containing a html form like :

<form id="payment" method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" value="_cart" name="cmd">
<input type="hidden" value="1" name="upload">
<input type="hidden" value="[email protected]" name="business">
<input type="hidden" value="Sample Item Name" name="item_name_1">
<input type="hidden" value="TESTI-1" name="item_number_1">
<input type="hidden" value="104.98" name="amount_1">
<input type="hidden" value="1" name="quantity_1">
<input type="hidden" value="0" name="weight_1">
<input type="hidden" value="Type" name="on0_1">
<input type="hidden" value="As Shown" name="os0_1">
<input type="hidden" value="Delivery Date" name="on1_1">
<input type="hidden" value="Jun 23,2012" name="os1_1">
<input type="hidden" value="Comments" name="on3_1">
<input type="hidden" value="test message" name="os3_1">
</form>

which contains the information that PayPal requires in order to process the order. Everything works fine except I believe sometimes the form gets submitted before the jQuery .html function is done with loading the html content.

Is there any callback function for .html ? or any other method that I can use to solve the issue ? the PayPal data comes as a HTML form and I can't change that part, so I only have one option which is somehow load the html content and submit the form !

6
  • 2
    First of all the response is not a json. It is HTML response, please check your code. Commented Jun 22, 2012 at 18:39
  • 1
    the .html method should be synchronous. As UmeshA pointed out, your problem is likely something else. Commented Jun 22, 2012 at 18:41
  • it is json, the only thing is that the html content is stored in jsonp['output'] as a string. the json object contains some other info as well. Commented Jun 22, 2012 at 18:43
  • that's what i was expecting but here is what happens, if i put a alert('bla bla'); between .html() and submit .. everything works fine ! Commented Jun 22, 2012 at 18:44
  • 1
    It doesn't make sense that it is happening at all, however a settimeout should fix it. setTimeout(function(){$('#payment').submit();},0); Commented Jun 22, 2012 at 18:45

1 Answer 1

47

You may try this

success: function(json) {
    $('#pp_info').html(json['output']).promise().done(function(){
        $('#payment').submit();
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

jQuery promise and detail.
hadn't heard of promise() before. Opens up a world of possibilities!
This does nothing more than setTimeout(fn) does. stackoverflow.com/questions/45085940/… the html method does not have a callback, and this promise isn't waiting on the html method to complete. It simply pushes the callback to the callback queue which causes it to be ran after the javascript in the html appended has ran. A setTimeout would be far more clear in this case rather than making people think .html() can have a callback.

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.