Advertisement
Guest User

ajaxcall

a guest
Jan 28th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     PROBLEM :
  3.     On FireFox : Everything running fine
  4.     On Internet Explorer (here 10) :
  5.         When the submit button is clicked, it fires first "onclick" function.
  6.         Console logs :
  7.         - button clicked
  8.         - No Errors
  9.         - Not generated yet
  10.         - ajax call
  11.  
  12.         AND STAY STUCKED THERE UNTIL I CLICK ON THE BUTTON AGAIN (without page refresh, only click)!
  13.        
  14.         On 2nd click, console logs :
  15.         - button clicked
  16.         - no errors
  17.         - not generated yet
  18.         - ajax call
  19.         - Error : [object Object] -  - {"readyState":4,"responseText":"","status":500,"statusText":"Internal Server Error"}
  20.         - called back
  21.         - button clicked
  22.         - no errors
  23.         - not generated yet
  24.         - ajax call
  25.         - function success
  26.         - called back
  27.         - button clicked
  28.         - no errors
  29.         - generated!
  30.  
  31.         AT THAT POINT, FORM IS SUBMITTED AND EVERYTHING OK.
  32.         WHY does it need 2 clicks on the button... why does ajax fail the first time and not second... :(
  33.         thanks for help / reading
  34. */
  35.  
  36.  
  37. //JAVASCRIPT :
  38.  
  39. $(document).ready(function(){
  40.     generated = false;
  41.     errors = true;
  42.     //somewhere in the code, when all fields are filled errors become false... (not important)
  43. });
  44.  
  45. $("body").on("click","#submitButton",function(event){
  46.     console.log("button clicked");
  47.     if(errors){
  48.         console.log("errors somewhere");
  49.         event.preventDefault();
  50.     }else{
  51.         console.log("no errors");
  52.         if(generated == false){
  53.             event.preventDefault();
  54.             console.log("not generated yet");
  55.             generatePdf(myData,function(){
  56.                 console.log("called back function");
  57.                 $("#myForm").submit();
  58.             });
  59.         }else{
  60.             console.log("already generated");
  61.             $("#myForm").submit();
  62.         }
  63.     }
  64. });
  65.  
  66. function generatePdf(myData,callback){
  67.     console.log("ajax call");
  68.     $.ajax({
  69.         type : "POST",
  70.         url : "./myFile.php",
  71.         data : {"myData":myData},
  72.         dataType : "json",
  73.         error : function(response){
  74.             console.log("error : "+response+" - "+response.responseText+" - "+JSON.stringify(response));
  75.         },
  76.         success : function(response){
  77.             console.log("function success");
  78.             generated = true;
  79.             callback();
  80.         }
  81.     });
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement