Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax发送多个请求而不是一个请求_Javascript_Jquery_Ajax - Fatal编程技术网

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

Javascript ajax发送多个请求而不是一个请求,javascript,jquery,ajax,Javascript,Jquery,Ajax,下面的代码最终导致每次单击发送的请求过多。我查看了我的Chrome浏览器的网络选项卡。我怎样才能修好它?我不知道为什么。 我基本上有一个ajax调用,它调用一个REST API,该API为我提供了所有名称的列表,我还有另一个与之匹配的REST API 比如说, /list gives me: list1,list2,list3 及 但是我有自己的代码,在其中循环遍历所有列表并调用/api/list1.json $.ajax({ dataType: 'json' url: '/l

下面的代码最终导致每次单击发送的请求过多。我查看了我的Chrome浏览器的网络选项卡。我怎样才能修好它?我不知道为什么。 我基本上有一个ajax调用,它调用一个REST API,该API为我提供了所有名称的列表,我还有另一个与之匹配的REST API

比如说,

/list gives me: list1,list2,list3

但是我有自己的代码,在其中循环遍历所有列表并调用/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
});
$.ajax({
数据类型:“json”
url:“/lists”,
成功:功能(数据){
如果(数据!=null){
var html='
    '; $.each(data.api,函数(i,项){ html+='
  • '; html+=''; html+='
  • '; }); html+='
'; $('#exDiv').empty().append(html); } }, 错误:函数(){ 警报(“错误”); }, contentType:'应用程序/json' }); $(文档).on('click','.id',函数(e){ e、 预防默认值(); var link=this.href; //在浏览器上显示.json的函数 });
使用keyup,而不是单击事件。当您按下按钮时,Click仍在发送事件。

如果我更改为keyup,例如preventDefault不起作用,我在这里看不到任何会发送多个AJAX请求的内容。我怀疑问题出在别的地方。顺便说一句,
$(this).next('.slidedown')
不会选择任何内容。
.slidedown
DIV不在
.id
之后,它在包含
.id
.hed
DIV之后。因此它应该是
$(this).最近的('.hed')。下一步('.slidedown')。slideto()
好的,我甚至删除了该代码,但问题是它仍然抛出很多请求。它发送了哪些请求
/lists
/api/listX.json
?是否可以在循环中调用
$(文档)。在('click'、'.id'、…)
$.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
});