jQuery的阿贾克斯beforeSend和成功,错误和放大器;完成放大器、错误、jQuery、阿贾克斯

2023-09-11 00:51:45 作者:少女的英雄梦

我在哪里第二阿贾克斯后的beforeSend是完成之前执行的AJAX功能的问题:第一个AJAX功能。我加入到placehorder发送之前加载类是工作第一Ajax调用。然而不久,第一个Ajax请求完成时删除了下课之后,再也没有追加在第二和进一步的调用(记住递归调用)。 虽然调试它表明的第二个AJAX调用beforeSend函数被调用最先,Ajax调用的功能齐全后来被调用。这是显而易见的,因为从第一个Ajax调用的页面插入返回的数据开始第二个电话。

在短期它的混淆。有什么办法这个可以整理出来?

函数code如下:

 函数AjaxSendForm(URL,占位符,形式,附加){
VAR数据= $(形式).serialize();
追加=(追加===未定义的假:真的吗?); //什么,它会评估为true或false只
$阿贾克斯({
    键入:POST,
    网址:网址,
    数据:数据,
    beforeSend:函数(){
        //设置超时
        $(占位符).addClass('装');
    },
    成功:功能(数据){
        如果(追加){
            $(占位符).append(数据);
        } 其他 {
            $(占位符)。html的(数据);
        }
    },
    错误:函数(XHR){//如果发生错误
        警报(错误occured.please重试);
        $(占位符).append(xhr.statusText + xhr.responseText);
        $(占位符).removeClass('装');
    },
    完成:函数(){
        $(占位符).removeClass('装');
    },
    数据类型:HTML
});
}
 

和数据中包含的JavaScript / jQuery的下面的代码片段,它检查并启动其他Ajax请求。

 <脚本类型=文/ JavaScript的> //<! - 
 $(文件)。就绪(函数(){
    $('#重启)。VAL(-1)
    $('#ajaxSubmit会)点击()。
});
//  - >< / SCRIPT>
 
3 jQuery AJAX

解决方案

也许你可以尝试以下方法:

 变种i = 0;
功能AjaxSendForm(URL,占位符,形式,附加){
VAR数据= $(形式).serialize();
追加=(追加===未定义的假:真的吗?); //什么,它会评估为true或false只
$阿贾克斯({
    键入:POST,
    网址:网址,
    数据:数据,
    beforeSend:函数(){
        //设置超时
        $(占位符).addClass('装');
        我++;
    },
    成功:功能(数据){
        如果(追加){
            $(占位符).append(数据);
        } 其他 {
            $(占位符)。html的(数据);
        }
    },
    错误:函数(XHR){//如果发生错误
        警报(错误occured.please重试);
        $(占位符).append(xhr.statusText + xhr.responseText);
        $(占位符).removeClass('装');
    },
    完成:函数(){
        一世 - ;
        如果(ⅰ&所述; = 0){
            $(占位符).removeClass('装');
        }
    },
    数据类型:HTML
});
}
 

这样一来,如果要是在完整语句我将超过0,所以它不会删除类。那么只有最后一次通话就可以将其删除。 我不能测试它,让我知道,如果它工作或没有。

I have a problem in ajax function where the beforeSend of second ajax post is executed before the complete: function of the first ajax. The loading class I am adding to the placehorder before sending is working for the first ajax call. However soon after the first ajax request completes the class is removed and never appends again on the second and further calls (remember recursive calls). While debugging it shows that the beforeSend function of the second ajax calls is called first and the complete function of the first ajax call is called later. Which is obvious because the return data inserted in the page from first ajax call starts the second call.

In short it's mixed up. Is there any way this can be sorted out?

The function code is as follows

function AjaxSendForm(url, placeholder, form, append) {
var data = $(form).serialize();
append = (append === undefined ? false : true); // whatever, it will evaluate to true or false only
$.ajax({
    type: 'POST',
    url: url,
    data: data,
    beforeSend: function() {
        // setting a timeout
        $(placeholder).addClass('loading');
    },
    success: function(data) {
        if (append) {
            $(placeholder).append(data);
        } else {
            $(placeholder).html(data);
        }
    },
    error: function(xhr) { // if error occured
        alert("Error occured.please try again");
        $(placeholder).append(xhr.statusText + xhr.responseText);
        $(placeholder).removeClass('loading');
    },
    complete: function() {
        $(placeholder).removeClass('loading');
    },
    dataType: 'html'
});
}

And the data contains the following snippet of javascript/jquery which checks and starts another ajax request.

<script type="text/javascript">//<!--
 $(document).ready(function() {
    $('#restart').val(-1)
    $('#ajaxSubmit').click();
});
//--></script>

解决方案

Maybe you can try the following :

var i = 0;
function AjaxSendForm(url, placeholder, form, append) {
var data = $(form).serialize();
append = (append === undefined ? false : true); // whatever, it will evaluate to true or false only
$.ajax({
    type: 'POST',
    url: url,
    data: data,
    beforeSend: function() {
        // setting a timeout
        $(placeholder).addClass('loading');
        i++;
    },
    success: function(data) {
        if (append) {
            $(placeholder).append(data);
        } else {
            $(placeholder).html(data);
        }
    },
    error: function(xhr) { // if error occured
        alert("Error occured.please try again");
        $(placeholder).append(xhr.statusText + xhr.responseText);
        $(placeholder).removeClass('loading');
    },
    complete: function() {
        i--;
        if (i <= 0) {
            $(placeholder).removeClass('loading');
        }
    },
    dataType: 'html'
});
}

This way, if the beforeSend statement if called before the complete statement i will be over 0 so it will not remove the class. Then only the last call will be able to remove it. I cannot test it, let me know if it works or no.