javascript - 没有从 Ajax 调用返回 JSON 数组

标签 javascript php ajax json

我知道之前有人问过这个问题,但我已经查看了我能找到的所有以前的帖子,但仍然无法让它发挥作用。希望问题很简单,你们可以帮我解决。

我无法从 PHP 脚本中获取一个数组,我已经对它进行了 json_encoded,并将其返回到 Javascript 数组中。如果我删除 dataType 应该是 json 的条件,那么成功函数会提示意外数据。

我有一个 Javascript 函数,它使用 POST 方法调用 PHP 脚本。在 PHP 脚本中,我将一些调试消息写入日志文件并显示 json_encoded 数组的内容。当我使用 JSONLint 检查内容是否符合 JSON 标准时,但我的 javascript 函数总是出错。

PHP 函数如下所示:

<?php

// Include the php logger class so that we can write log messages to a log file
require_once('/myscripts/phplogger.php');

// Set up php log file
$log = new Logging();
$log->lfile('/mylogfiles/php-debug.log');

// Log status
$log->lwrite('Starting debug');
// ...
// (there's some more stuff here to get the data from a MySQL database)
// ...

// The following line when read in the log file shows a valid JSON array
$log->lwrite('array '.json_encode($dataVnvMethods));

$json_dataVnvMethods = json_encode($dataVnvMethods);

$log->lwrite('Ending debug');

?>

Javascript 函数如下所示:

function jsgetvnvfilters(projid,repid,weekid)
{
    $.ajax(
            {
            url: './getvnvfilter.php',
            data: {'projid':projid, 'weekid':weekid, 'repid':repid},
            type: 'post',
            dataType: 'json',
            success: function()
              {
                    alert('success');
                    var prevnvlist = '<?php echo $json_dataVnvMethods ?>';
                    var vnvlist = JSON.parse(prevnvlist);
                    for (var x = 1; x <= vnvlist.length; x++) {
                            var vnv = vnvlist[x]['VnVMethod'];
                            vnvSel.options[vnvSel.options.length] = new Option(vnv, vnv);
                    }
              },
            error: function()
              {
                    alert('failure');
              }

            }
    );

}

最佳答案

你误解了 javascript 和 php 的关系; php 只会在页面加载时呈现一次,因此您不能在 javascript success 处理程序中echo 返回的 php 数据。

相反,您的 php 脚本输出的所有内容都将在 javascript 变量中可用:

success: function(vnvlist) {
           //     ^^^^^^^ this is what has been outputted by php, the
           //             json already parsed

           // your data is available in `vnvlist`
           // var prevnvlist = '<?php echo $json_dataVnvMethods ?>';

           // with dataType='json' you don't need to parse anything
           // as jQuery will parse it for you
           // var vnvlist = JSON.parse(prevnvlist);

           // so all you need is this...
           alert('success');
           for (var x = 1; x <= vnvlist.length; x++) {
             var vnv = vnvlist[x]['VnVMethod'];
             vnvSel.options[vnvSel.options.length] = new Option(vnv, vnv);
           }
         },

并且您需要确保 php 仅输出您的 json 字符串:

...
// The following line when read in the log file shows a valid JSON array
$log->lwrite('array '.json_encode($dataVnvMethods));

// output your data so that it is available on the client-side
echo json_encode($dataVnvMethods);

$log->lwrite('Ending debug');
...

关于javascript - 没有从 Ajax 调用返回 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28151696/

相关文章:

javascript - Sequelize hasMany 关系的 getAssociation

javascript - HTML 表 : Content with minus symbol breaks line in the column

php - mysql多表查询返回排序结果发送查看之前

PHP & MySQL 循环问题

ajax - ajax 应用程序中 Internet Explorer 11 的保活问题

javascript - 如何仅在 Promise 完成后才执行函数?

javascript - 在javascript中旋转时钟指针

php - javascript中的ie8 split函数问题

javascript - 如何在不重新加载页面的情况下更新 Django 页面?

ruby-on-rails - 如何在 RAILS 中使用 AJAX 销毁微博时重新加载当前页面