簡體   English   中英

Ajax成功函數在離開范圍之前不等待內部腳本完成

[英]Ajax success function not waiting on internal script to complete before leaving scope

我有這段代碼:

$.ajax({
  type: "POST",
  async: true,
  url: "Notes/ViewAttachments.aspx/CompressFiles",
  data: "{ 'hidBinaryFileIDs': '" + csList + "', 'userID' : '" + userID + "'}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (data) {
    $.Zebra_Dialog(data.d, {
      'type': 'information',
      'title': 'Confirmation',
      'buttons': [{
        caption: 'Ok',
        callback: function () {

        }
      }]
    });
  },
  error: function (xhr, ajaxOptions, thrownError) {
    $.Zebra_Dialog('Error : ' + xhr.status + ' - ' + xhr.statusText + ' : ' + thrownError, {
      'type': 'error',
      'title': 'Confirmation',
      'buttons': [{
        caption: 'Ok',
        callback: function () {}
      }]
    });
  }
});

當ajax成功返回時,它將顯示整個對話框大約2秒鍾(對於用戶來說,閱讀該消息的時間不夠長),然后將其關閉。 使用chrome的調試器,我確定它已超出范圍,而無需等待成功函數內部對話框上的確認。 有誰知道在用戶單擊確定之前我將如何暫停代碼?

這是該ajax調用的完整代碼塊。

 var leZDB = null;
    function zipSelectedAttachments() {

        var ids = getSelectedTaskIDs();
        if (ids.length > 0) {
            leZDB = $.Zebra_Dialog('Do you want to zip the attachment(s)?', {
                'type': 'question',
                'title': 'Confirmation',
                'buttons': ['Yes', 'No'],
                'onClose':function (caption) {
                    if(caption = 'Yes') {
                        LinkAndPass(ids);
                    }
                }
            });
        } else {
            $.Zebra_Dialog('No attachments are selected.', {
                'type': 'error',
                'title': 'Error',
                'buttons': [
                    { caption: 'Ok', callback: function () { } }

                ]
            });
        }
    }

    function LinkAndPass(ids) {
        leZDB.close();
        if (true)
        {
            SendIDSForZipping(ids);
        }
    }

    function SendIDSForZipping(ids) {
        var csList = '';
        var userID = $('#hiduserID').val();

        for (var i = 0; i < ids.length; ++i) {
            csList += ids[i] + ',';
        }
        var $this = $(this);
        $this.die('click');

        $.ajax({
            type: "POST",
            //async: true,
            url: "Notes/ViewAttachments.aspx/CompressFiles",
            data: "{ 'hidBinaryFileIDs': '" + csList + "', 'userID' : '" + userID+ "'}",
            contentType: "application/json; charset=utf-8",
            context:this,
            dataType: "json",
            success: function (data) {
                var hasClickedOK = false;
                var hasDisplayed = false;
                    if (!hasDisplayed) {
                        hasDisplayed = true;
                        $.Zebra_Dialog(data.d, {
                            'type': 'information',
                            'title': 'Confirmation',
                            'buttons': [
                                {
                                    caption: 'Ok',
                                    callback: function () {
                                        hasClickedOK = true;
                                    }
                                }
                            ]
                        });
                    }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $.Zebra_Dialog('Error : ' + xhr.status + ' - ' + xhr.statusText + ' : ' + thrownError, {
                    'type': 'error',
                    'title': 'Confirmation',
                    'buttons': [
                        {
                            caption: 'Ok',
                            callback: function () {
                            }
                        }
                    ]
                });
            }
        });
    }

getSelectedIDs()返回一個數字數組。

后面的代碼返回一個字符串。

$.ajax({ type: "POST", async: true });嘗試asyn:false $.ajax({ type: "POST", async: true });

問題是從后面的代碼中調用了web方法。 錯誤的邏輯導致其刷新頁面,立即關閉該框。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM