简体   繁体   中英

jQuery deferred method and ajax beforeSend()

By using the deferred object in $.ajax

  • the success-callback can replaced by the deferred-method done()
  • the error-callback acn replaced by the deferred-method fail()
  • and the complete-callback can by replaced by always()

by using

var jqxhr = $.ajax({
                url: Config.baseUrl+"/ajax/favourites/set-favourite.ajax",
                dataType: "json",
                data: attrs,
                type: "POST",
                beforeSend: function(){
                    console.log("before send");
                }
            });

how can i implement the beforeSend-callback by using the deferred object?

why i don't use the beforeSend-callback inside the $.ajax function? Because the request is inside a Model-Instance ( http://canjs.com/docs/can.Model.model.html#section_Non_standardServices ) so the model object do the request and all the other suff, like manipulate the DOM will done in the deferred object. i would like to manipulate the DOM before sending the ajax request.

how could i do that?

You could attach the global jQuery event ajaxStart to whatever element is triggering your AJAX request. That should essentially replicate the functionality of beforeSend .

$(".ajax").ajaxStart(function() {
    $(".document").append("AJAX begun");
});

Fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM