Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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
Php 将Ajax数据打印到html表时出错_Php_Ajax - Fatal编程技术网

Php 将Ajax数据打印到html表时出错

Php 将Ajax数据打印到html表时出错,php,ajax,Php,Ajax,我有一个php函数来生成数字列表,还有一个ajax调用来检索数字数组。我可以提醒列表,它可以正常工作,但是当我尝试将它打印到HTML表中时,我会得到错误UncaughtTypeError:无法使用'in'运算符在无穷远处搜索'length' 任何帮助都将不胜感激 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1

我有一个php函数来生成数字列表,还有一个ajax调用来检索数字数组。我可以提醒列表,它可以正常工作,但是当我尝试将它打印到HTML表中时,我会得到错误UncaughtTypeError:无法使用'in'运算符在无穷远处搜索'length' 任何帮助都将不胜感激

<!DOCTYPE html>
<html>
<head>
<script     src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<script>
$.ajax({
type: "POST",
url: "primeNumbers.php",
datatype: 'JSON',
success: function(data){
    var d = $.each(JSON.parse(data));
    var output;
    $.each(d,function(i,e){
        output += '<tr><td>'+e.data+'</tr></td>';
        });

    $('#table').append(output);

    alert(data);
}
});
</script>
<h1>Heading</h1>
<table id="table">
  <tr>
    <td>Name</td>
  </tr>
</table>

</body>
</html>
primeNumbers.php

<?php
function prima($n){

  for($i=1;$i<=$n;$i++){

          $counter = 0; 
          for($j=1;$j<=$i;$j++){ 


                if($i % $j==0){ 

                      $counter++;
                }
          }


        if($counter==2){

               echo json_encode($i);
        }

    }
} 
prima(100);  

?>

实际错误意味着$.each可能获取了错误的数据类型。例如,在中的字符串应该传递给它可以迭代的对象。在您的例子中,javascript和PHP代码都有一些错误。您的PHP代码只是回显了质数。因此,ajax函数返回了一个串联的数字字符串,这是导致未捕获类型错误的原因。您必须将数字推送到一个数组中,将其转换为json字符串并返回结果,这样您就可以在任何需要的地方回显它

只考虑ajax函数。变量声明中的每一个松散$。 因此:

变成:

var d = JSON.parse(data);
更新添加了PHP修复程序

下面是修复/重构的PHP函数

function prima($n){

   $res = []; // Initiate result array

   for($i=1;$i<=$n;$i++){

      $counter = 0; 
      for($j=1;$j<=$i;$j++){ 


            if($i % $j==0){ 

                  $counter++;
            }
      }


    if($counter==2){
         $res[] = $i; // store value to array
    }

  }

  return json_encode($res); // return converted json object

}

header('Content-Type: application/json'); // tell browser what to expect
echo prima(100); // echo the json string returned from function

您看过php脚本的输出了吗?它只是输出一个非常大的数字。你必须在php脚本中将素数放入一个数组,然后输出该数组的json版本。顺便说一句:这是一种非常低效的计算素数的方法是的,我曾经尝试过这种方法。我就是好像过不了这关error@user2168066更新答案以包含您的固定功能
function prima($n){

   $res = []; // Initiate result array

   for($i=1;$i<=$n;$i++){

      $counter = 0; 
      for($j=1;$j<=$i;$j++){ 


            if($i % $j==0){ 

                  $counter++;
            }
      }


    if($counter==2){
         $res[] = $i; // store value to array
    }

  }

  return json_encode($res); // return converted json object

}

header('Content-Type: application/json'); // tell browser what to expect
echo prima(100); // echo the json string returned from function