weixin_33720186 2017-03-22 13:28 采纳率: 0%
浏览 2

$ .ajax奇怪的订单呼叫

First i apologize for my future half-level in english.

I came to you cause i have a weird issue with my javascript.

Here you can see that i have this code :

addVoucherMaster: function (Code) {
    var btns = $('.btn-spin');
    btns.empty();
    if (btns.find('i.fa-spinner').length == 0) {
        $('<i>', { 'class': 'fa fa-spinner fa-pulse' }).appendTo(btns);
    }
    btns.css('opacity', '.5');
    $(".validation-summary-errors").attr('class', 'validation-summary-valid');
    $(".validation-summary-valid").children('ul').empty();

    var dataSend = {
        voucherId: Code,
    };
    var jsonData = JSON.stringify(dataSend);

    var result = null;
    if (Code && this.addVoucherUrl) {
        $.ajax({
            url: this.addVoucherUrl,
            type: 'POST',
            data: jsonData,
            dataType: 'json',
            processData: false,
            contentType: 'application/json; charset=utf-8',
            async: false,
            success: function (data) {
                result = data.Data;
                return data;
            }
        });
        if (result == 'True') {
            this.payments();
        } else {
            var btns = $('.btn-spin');
            this.errorVoucher("Incorrect voucher");
            btns.css('opacity', 1);
            btns.empty();
            btns.append('+');
        }
    }
},

This is really bad written, but first i replace the "+" in a button with a spinner ( loader ), then i make an ajax query (async) and reexecute some code after. (all of this is executed by a data-bind="click:addVoucherMaster")

Issues start here. When i use the Chrome debugger with breakpoints, after the 'btns.css('opacity', '.5');', my button is like i want, then the responses make me wait for 1/2 seconds, and my button stop to spin at the end.

But, without debugger, the 1/2 seconds appears before my buttons start spinning. ( so it starts and stop at the same time, we don't see anything ).

Is anyone able to help me ??

Thank you in advance,

David,

  • 写回答

2条回答 默认 最新

  • weixin_33709364 2017-03-22 13:59
    关注

    Change the Ajax request to this:

    $.ajax({
        url: this.addVoucherUrl,
        type: 'POST',
        data: jsonData,
        dataType: 'json',
        processData: false,
        contentType: 'application/json; charset=utf-8',
        async: false,
        success: function (data) {
            result = data.Data;
    
            if (result == 'True') {
                this.payments();
            } else {
                var btns = $('.btn-spin');
                this.errorVoucher("Incorrect voucher");
                btns.css('opacity', 1);
                btns.empty();
                btns.append('+');
            }
        }
    });
    

    The if / else structure you had was probably executed while the ajax request was still busy.

    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错