ajax发送许多请求而不是一个

时间:2015-12-22 16:09:03

标签: javascript jquery ajax

以下代码最终会为每次点击发送过多请求。我查看了Chrome的网络标签。我该如何解决?我不知道为什么。 我基本上有一个调用REST API的ajax调用,它给出了所有名称的列表,我有另一个与之匹配的REST API。

例如,

/list gives me: list1,list2,list3

/api/list1.json gives me: json of list1..

但我有我的代码,我循环遍历所有列表并调用/api/list1.json

$.ajax({
    dataType: 'json'
    url: '/lists',
    success: function (data) {
        if (data != null) {
            var html = '<ul>';
            $.each(data.apis, function (i, item) {
                html += '<li class="res">';
                html += '<div class="hed"><h2><a class="id" href="/api/' + item + '.json">' + item + '</a></h2></div><div class="slidedown"></div>';
                html += '</li>';
            });
            html += '</ul>';
            $('#exDiv').empty().append(html);
        }
    },
    error: function () {
        alert('Error');
    },
    contentType: 'application/json'
});

$(document).on('click','.id', function(e) {
   e.preventDefault();
   var link = this.href; 

   //function that displays the .json on the browser
});

1 个答案:

答案 0 :(得分:0)

使用keyup而不是单击事件。当您按下按钮时,单击仍然发送事件。