javascript – 没有从Ajax调用中获取JSON数组

我知道之前已经问过这个问题,但我已经回顾了以前可以找到的所有帖子,但仍然无法使用它.希望问题很简单,你们可以帮我解决.

我无法从PHP脚本中获取一个数组,我将json_encoded返回到Javascript数组中.如果我删除dataType应该是json的条件,那么success函数会抱怨意外的数据.

我有一个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成功处理程序中回显返回的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');
...

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...