0

我有这段代码:

$.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() 返回一个数字数组。

后面的代码返回一个字符串。

4

2 回答 2

0

尝试 asyn:false in$.ajax({ type: "POST", async: true });

于 2012-09-18T15:58:35.453 回答
0

问题在于从后面的代码调用 Web 方法。一个错误的逻辑导致它刷新页面,立即关闭框。

于 2012-09-25T19:53:22.630 回答