Ajax response expected



  • Hello. At the beginning, I need to send ajax request, get the data from him, and then continue the violin. I've got this code now. var user_data = getdata(count); The method getdata sends the call to the crypt. The problem is, when the variable user_data needs it, it's still empty, because we didn't get the answer from the server. How to do this? parse


  • QA Engineer

    You can use jackverev $.when and his promise https://api.jquery.com/jquery.when/

    If $.when() I'll give you one rescheduled object, he'll just return his promise-version. In addition to the deferred $.when() any other js objects may be indicated. jQuery They will always be determined as successful.

    Example:

    $.when( $.ajax("test.aspx") ).then(function(data, textStatus, jqXHR){
        alert( jqXHR.status ); // выводит 200
    });
    

    That's why you're drinking. when Your request, and then you finish your dirty business:

    $.when( $.ajax( "/page1.php" ) ).done(function( data ) {
        // делаете что-то что вам нужно сделать
        // var response = data[ 0 ] + data[ 1 ]; 
        // alert(response);
        // ...
        // ...
    });
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2