簡體   English   中英

如何從Ajax調用僅輸出json_encoded數組?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM