PHP:json_en code()的SyntaxError是否应该使用include_once的()json_en、PHP、code、include_once

2023-09-10 14:32:38 作者:朕射伱无罪

备注:使用XAMPP 在PHP中我使用 json_en code()将数据返回到JS / JQuery的Ajax请求。

REMARK: USING XAMPP On PHP I'm using json_encode() to return data to a JS/JQuery ajax request.

从.js文件Ajax调用:

The ajax call from .js:

$.ajax({url: "/sys/search_rf/functions.php" ,
    async: false, // wait for reponse
    type: "post",
    data: { id: $('#id').val(),
            status: $('#status').val(),
            token: cToken } ,
    success: function(jsonResponse,status){ // POST success
        console.log(jsonResponse);
    jsonData = JSON.parse(jsonResponse);
            [...]
        }

它工作得很好,直到我 require_once()中的functions.php另一个.PHP。

It works just fine until I require_once() another .php in functions.php.

require_once(dirname(dirname(__FILE__)).'/searches.php'); // -> ../searches.php

输出调试:

Output debug:

没有一个包括PHP的回声/打印任何东西,除了它返回功能 json_en code()

None of the included PHP echoes/print anything at all, except the function which returns the json_encode().

于是,我编辑searches.php,离开它像这样(调试目的):

So then, I edited searches.php and left it like this (debugging purpose):

<?php

?>

所以,我100%肯定没有什么呼应,我不停:

So I'm 100% sure nothing is echoing, and I kept:

require_once(dirname(dirname(__FILE__)).'/searches.php'); // -> ../searches.php

在functions.php的,这是为了响应Ajax请求。

in functions.php, which responds to ajax request.

但错误剧照:

SyntaxError: JSON.parse: unexpected character

如果我评论它,就没有错误!

And if I comment it, there will be no errors!

// require_once(dirname(dirname(__FILE__)).'/searches.php'); // -> ../searches.php

我想我每次 require_once()是回声的东西。我真的不知道。

I suppose every time I require_once() it 'echoes' something. I don't really know.

我也checkd所有文件的所有EOL /编码。 在所有的Unix格式EN codeD的UTF-8。

I also checkd all EOL/Encoding of all files. All in Unix Format encoded in UTF-8.

此解决方法会做,但我认为这不是正确的解决办法:

jsonData = JSON.parse( jsonResponse.trim() );

.trim()

.trim()

推荐答案

您应该删除关闭PHP的标签,并打开PHP标签之前的所有空间。还要检查,如果你在你的文件中使用UTF-8无BOM。

You should remove closing php tag and all spaces before opening php tag. Also check if you use UTF-8 without BOM in your file.