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

时间:2022-03-27 07:37:33

I know this has been asked before but I've reviewed all the previous posts I can find and still cannot get this to work. Hopefully the problem is simple and you guys can help me solve it.

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

I am not able to get an array, which I have json_encoded, from the PHP script back into a Javascript array. If I remove the condition that dataType should be json then the success function complains about unexpected data.

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

I have a Javascript function which calls a PHP script with POST method. In the PHP script I write some debugging messages to a log file and to display the content of the json_encoded array. When I check that using JSONLint the content is JSON compliant but my javascript function always goes to error.

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

The PHP function looks like:

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

?>

The Javascript function looks like:

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

            }
    );

}

3 个解决方案

#1


4  

You misunderstand how the javascript and php are related; the php will be rendered only once on page-load so you cannot echo the returned php data in your javascript success handler.

你误解了javascript和php是如何相关的; php将在页面加载时仅呈现一次,因此您无法在javascript成功处理程序中回显返回的php数据。

Instead, everything that is outputted by your php script will be available in a javascript variable:

相反,您的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);
           }
         },

And you need to make sure that php outputs only your json string:

而且你需要确保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');
...

#2


0  

Press f12 and open developer tools. Go to section network. And then try. If you got 404 error it means that you enter wrong URI or something. Basically, if Ajax return you error it means you didn't access your script for some reason. Maybe you entered wrong URI, or you don't have permission.

按f12并打开开发人员工具。转到部分网络。然后试试。如果您收到404错误,则表示您输入了错误的URI或其他内容。基本上,如果Ajax返回错误,则意味着您由于某种原因没有访问您的脚本。也许您输入了错误的URI,或者您没有权限。

#3


0  

use data: JSON.stringify(value[, replacer [, space]])

使用数据:JSON.stringify(value [,replacer [,space]])

#1


4  

You misunderstand how the javascript and php are related; the php will be rendered only once on page-load so you cannot echo the returned php data in your javascript success handler.

你误解了javascript和php是如何相关的; php将在页面加载时仅呈现一次,因此您无法在javascript成功处理程序中回显返回的php数据。

Instead, everything that is outputted by your php script will be available in a javascript variable:

相反,您的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);
           }
         },

And you need to make sure that php outputs only your json string:

而且你需要确保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');
...

#2


0  

Press f12 and open developer tools. Go to section network. And then try. If you got 404 error it means that you enter wrong URI or something. Basically, if Ajax return you error it means you didn't access your script for some reason. Maybe you entered wrong URI, or you don't have permission.

按f12并打开开发人员工具。转到部分网络。然后试试。如果您收到404错误,则表示您输入了错误的URI或其他内容。基本上,如果Ajax返回错误,则意味着您由于某种原因没有访问您的脚本。也许您输入了错误的URI,或者您没有权限。

#3


0  

use data: JSON.stringify(value[, replacer [, space]])

使用数据:JSON.stringify(value [,replacer [,space]])