当连续调用与ajax调用相同的函数时,需要有关ajax的帮助

时间:2014-05-21 15:39:11

标签: javascript jquery ajax

我需要有关ajax的帮助,因为我是新手。当ajax调用完成执行之前多次调用相同的ajax调用时,它会在ui上弹出空弹出。我怎么解决这个问题。实际上当用户点击下载链接时,该代码是在ajax的成功函数中编写的。但是,如果用户在下载第一个链接之前多次单击下载链接。它的投掷错误。我该如何解决这个问题?

这是我的代码

function clickLink(section) {

    $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType: 'text',
        url: 'MyService.asmx/getMetrics',
        data: { programName: progName, startYear: beginYear, endYear: endYear, userName: spnUserName.innerHTML, rdate:dt },
        success: function (data) {
            var jdata = data;
            jdata = jdata.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");
            jdata = jdata.replace('<?xml version="1.0" encoding="utf-8"?>', '');
            jdata = jdata.replace('\r\n', '');        
            var link = document.createElement("a");           
            link.setAttribute('href', jdata.trim());
            link.onclick = OpenFile(jdata.trim());
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}

2 个答案:

答案 0 :(得分:0)

您可以设置临时课程。

$('.your-element:not(.disabled)').click(function(){
    var $this = $(this);
    $this.addClass('disabled');

    $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType: 'text',
        url: 'MyService.asmx/getMetrics',
        data: { programName: progName, startYear: beginYear, endYear: endYear, userName: spnUserName.innerHTML, rdate:dt },
        success: function (data) {
            var jdata = data;
            jdata = jdata.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");
            jdata = jdata.replace('<?xml version="1.0" encoding="utf-8"?>', '');
            jdata = jdata.replace('\r\n', '');        
            var link = document.createElement("a");           
            link.setAttribute('href', jdata.trim());
            link.onclick = OpenFile(jdata.trim());
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(errorThrown);
        }
    }).done(function(){
       $this.removeClass('disabled');
    });

    return false;
});

答案 1 :(得分:0)

将ajax函数声明为变量,然后使用函数并检查变量是否已定义:

var xhr;
function CallAjax(){
    if( xhr !== undefined ) return false;
    xhr = $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType: 'text',
        url: 'MyService.asmx/getMetrics',
        data: { programName: progName, startYear: beginYear, endYear: endYear, userName: spnUserName.innerHTML, rdate:dt },
        success: function (data) {
            var jdata = data;
            jdata = jdata.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");
            jdata = jdata.replace('<?xml version="1.0" encoding="utf-8"?>', '');
            jdata = jdata.replace('\r\n', '');        
            var link = document.createElement("a");           
            link.setAttribute('href', jdata.trim());
            link.onclick = OpenFile(jdata.trim());
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}