Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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/77.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 - Fatal编程技术网

Javascript 元素内的$.ajax()调用。每个()函数都会导致“太多递归”错误

Javascript 元素内的$.ajax()调用。每个()函数都会导致“太多递归”错误,javascript,jquery,Javascript,Jquery,下面的代码旨在查找元素,.placeholder在本例中,从该元素获取属性data\u id,然后使用该值异步请求html片段,如果成功,该片段将附加到元素 <script> jQuery( document ).ready(function( $ ) { var params = {}; $( ".placeholder" ).each(function(i){ params['id'] = $(this).attr('data_id'); $.aj

下面的代码旨在查找元素,.placeholder在本例中,从该元素获取属性data\u id,然后使用该值异步请求html片段,如果成功,该片段将附加到元素

<script>
jQuery( document ).ready(function( $ ) {
  var params = {};
  $( ".placeholder" ).each(function(i){
      params['id'] = $(this).attr('data_id');
      $.ajax({
          url: 'http://example.com/endpoint',
          type: 'POST',
          data: params
      })
      .done(function(html){
          $(this).html( html ); 
      });
  });
});
</script>
<body>
<div class="placeholder" data_id="1234"></div>
</body>

当我运行这个程序时,它失败了,Firefox使用“太多递归”并指向jQuery源代码。我的代码中是否存在问题,或者至少有一种方法可以调试导致问题的原因?

您在完成回调中的.placeholder元素上的引用错误

尝试:


done回调的内部引用了没有html函数的jqXHR对象$jqXHR添加了html函数,但它不起作用,因为底层对象不是DOM元素。

如果有500.placeholder,则一次可以启动500个ajax请求。。。也许不是个好主意。@jeremy只有一个1@Vohuman我进行了编辑,以便清楚地知道参数是什么is@charlietfl-params是{id:1234}的键/值对。如何调试它?请尝试删除此行元素.html;。它能消除错误吗?如果是,你知道你的问题来自哪里。无论如何,您必须提供最低限度的示例代码来复制您的问题。仅供参考,在这种情况下,使用.empty是无用的
  var params = {};
  $( ".placeholder" ).each(function(i){
      var that = $(this);
      params['id'] = that.attr('data_id');
      $.ajax({
          url: 'http://example.com/endpoint',
          type: 'POST',
          data: params
      })
      .done(function(html){
          that.html( html ); 
      });
  });