jQuery AJAX request events - done,fail,success

zmajeric

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?

Pranay Rana

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 );
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

jQuery ajax() using success, error and complete vs .done(), .fail() and always()

From Dev

Does jQuery ajax request always call done or fail methods?

From Dev

Jquery ajax done and fail not fireing

From Dev

jquery ajax request success portion not working

From Dev

Jquery: Ajax POST request / success event not fired

From Dev

Including Success / Fail to jQuery / Ajax function

From Dev

jQuery Ajax Success nor Fail methods hit

From Dev

jQuery ajax fail always triggered (even in success)

From Dev

Server Side Events & Ajax Request

From Dev

Changing ajax POST data for a second request using .done() instead of success

From Dev

jQuery .ajax calls success function before sending request

From Dev

jQuery ajax request: how to access sent data in success function?

From Dev

Success message is not showing in html page with ajax/jquery request

From Dev

jQuery - Use variable in AJAX request for it's success callback

From Dev

Why jQuery consider an 200 response Ajax request with empty content as fail()?

From Dev

JQuery ajax success fired twice, because of .done callback

From Dev

Angular and jQuery Ajax Request

From Dev

JQuery not working on AJAX Request

From Dev

CORS jQuery AJAX request

From Dev

Javascript to jquery Ajax request

From Dev

ajax request django , jquery

From Dev

JQuery Ajax Request with Percentage

From Dev

HTTP request jQuery AJAX

From Dev

Jquery after Ajax request

From Dev

CORS jQuery AJAX request

From Dev

Jquery Ajax Request Not Working

From Dev

jQuery - AJAX Request

From Dev

AJAX POST request with JQUERY

From Dev

jQuery Ajax request inside Ajax request not working

Related Related

HotTag

Archive