没有从Ajax调用获取JSON数组

use*_*506 3 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');

?>
Run Code Online (Sandbox Code Playgroud)

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');
              }

            }
    );

}
Run Code Online (Sandbox Code Playgroud)

jer*_*oen 5

您误解了javascript和php的关系;该php仅在页面加载时呈现一次,因此您无法echo在javascript success处理程序中返回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);
           }
         },
Run Code Online (Sandbox Code Playgroud)

并且您需要确保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');
...
Run Code Online (Sandbox Code Playgroud)