简体   繁体   English

jQuery延迟方法和ajax beforeSend()

[英]jQuery deferred method and ajax beforeSend()

By using the deferred object in $.ajax 通过在$ .ajax中使用延迟对象

  • the success-callback can replaced by the deferred-method done() 成功回调可以用deferred-method done()代替
  • the error-callback acn replaced by the deferred-method fail() 错误回调acn被deferred-method fail()
  • and the complete-callback can by replaced by always() 并且完整回调可以替换为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? 如何使用延迟对象实现beforeSend-callback

why i don't use the beforeSend-callback inside the $.ajax function? 为什么我不在$.ajax函数中使用beforeSend-callback? 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. 因为请求在模型实例( http://canjs.com/docs/can.Model.model.html#section_Non_standardServices )内,所以模型对象执行请求和所有其他的后缀,比如操作DOM将在延期对象。 i would like to manipulate the DOM before sending the ajax request. 我想在发送ajax请求之前操纵DOM。

how could i do that? 我怎么能这样做?

You could attach the global jQuery event ajaxStart to whatever element is triggering your AJAX request. 您可以将全局jQuery事件ajaxStart到触发AJAX请求的任何元素。 That should essentially replicate the functionality of beforeSend . 这应该基本上复制beforeSend的功能。

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

Fiddle . 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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