PHP:json

编程入门 行业动态 更新时间:2024-10-28 11:23:13
本文介绍了PHP:json_en code()的SyntaxError是否应该使用include_once的()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

备注:使用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.

更多推荐

PHP:json

本文发布于:2023-05-27 19:05:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/299873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!