【发布时间】:2017-02-23 10:29:43
【问题描述】:
我有一个 php 函数来生成数字列表,以及一个 ajax 调用来检索该数字数组。我可以提醒列表并且它工作正常,但是当我尝试将其打印到 HTML 表中时,我收到错误“未捕获的 TypeError:无法使用 'in' 运算符在 Infinity 中搜索 '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);
?>
【问题讨论】:
-
你看过你的 php 脚本的输出了吗?它只是输出一个非常大的数字。您必须将素数放入 php 脚本中的数组中,然后输出该数组的 json 版本。
-
顺便说一句:这是一种非常低效的计算素数的方法