[英]How to output only a json_encoded array from an Ajax call?
我希望我的ajax调用仅从test.php中的数组返回值
目前,ajax调用正在返回php文件中存在的所有代码。
如何仅返回json_encoded数组?
jQuery代码:
var params = {'action': 'save' , 'id':5};
$.ajax({
type: "POST",
url: "test.php",
data: params,
success: function( data ) {
$.each(data, function (index, value) {
$('#menu_container a').eq( index).text( value);
});
}
});
test.php:
<?php
$array = array();
$i = 0;
while ($i < $num) {
$f1 = mysql_result($result, $i, "Page");
$array[] = $f1; ?>
<?php echo $f1; ?>
<?php $i++;
}
?>
</br>
</br>
<?php
echo json_encode($array);
?>
</body>
</html>
只需删除PHP代码中生成输出的所有其他部分:
<?php
$array = array();
$i = 0;
while ($i < $num) {
$f1 = mysql_result($result,$i,"Page");
$array[] = $f1;
$i++;
}
echo json_encode($array);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.