Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX request events - done,fail,success

I have code like that

var ajaxrequest = $.ajax({
            type: "POST",
            dataType: "json",
            url: "xy.php", 
            data: {
                action : "read"
            }
            }).fail(function(){
                //something to do when ajaxreq fails
            }).done(function(data){

               //something to do when ajaxreq is done
            });

It is working no problem. My question is why this doesnt work:

var ajaxrequest = $.ajax({
            type: "POST",
            dataType: "json",
            url: "n3_vaje_api.php", //Relative or absolute path to response.php file
            data: {
                action : "read",
            },
            fail:function(){
                //something to do when ajaxreq fails
            },
            done:function(data){
              //something to do when ajaxreq is done
            }
        });

Fail and done are just examples, complete also doesnt work if used inside. But using it outside like:

ajaxrequest.complete(f(){});

is working just fine... I know instead of done I should use success, but thats not my point here. Whats the deal here?

like image 476
zmajeric Avatar asked Jan 20 '15 11:01

zmajeric


People also ask

What is done () in AJAX call?

The done() function is given a function as parameter. The callback function passed as parameter to the done() function is executed if the AJAX request succeeds. The callback function gets three parameters: data , textStatus and jqXHR . The data parameter is the data returned by the server.

Does AJAX need a success function?

The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds. The function takes three parameters, namely: The data returned from the server, formatted according to the dataType parameter, or the dataFilter callback function.

How do I know AJAX call is complete?

The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.

How would you fire a callback when any AJAX request on a page has completed?

The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.


1 Answers

you need to use success and error is the method you need to use if you want to use your second option

this is example of ajax request without promise, where you are getting success and error function as parameter

 $.ajax({url:"demo_test.txt"
      ,error : function (xhr,status,error)
        { //alert error}
      ,success:function(result){
      $("#div1").html(result);
    }});

In the first opetion you are using promise object return by ajax requst that is the reason you are getting done and fail method.

this is example of promise object , in below example request is promise object

var request = $.ajax({
  url: "script.php",
  type: "POST",
  data: { id : menuId },
  dataType: "html"
});

request.done(function( msg ) {
  $( "#log" ).html( msg );
});

request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});
like image 118
Pranay Rana Avatar answered Sep 30 '22 06:09

Pranay Rana