Ajax 问题、无效 JSON
我正在构建简单的 Ajax 应用程序(通过 jquery)。我有奇怪的问题。我找到了问题所在,但不知道如何解决。
这是简单的服务器端 php 代码:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
在客户端,抛出错误“无效的 JSON”。
我发现如果我删除 require 函数,一切都会正常工作。
仅供参考,“some.php”是一个空的 php 文件。直接打开php文件没有报错。
所以,结论:如果我想使用ajax,我不能使用require或include函数吗?
I'am building simple Ajax application (via jquery). I have strange issue. I found where the problem is, but I don't know how to solve it.
This is simple server-side php code:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
On the client side, the error "Invalid JSON" is thrown.
I have discovered that if I delete require function, everything work fine.
Just for information, the "some.php" is an empty php file. There is no error when I open direct php files.
So, conclusion: I cant use require or include function if I want to use ajax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 Firebug 查看 AJAX 调用期间实际返回的内容。我的猜测是某处存在 PHP 错误,因此您从调用中获得的不仅仅是 JSON(Firebug 会向您展示这一点)。至于你的结论:单独使用 include/require 对 AJAX 调用绝对没有影响(假设没有错误)。
Use Firebug to see what you're actually getting back during the AJAX call. My guess is that there's a PHP error somewhere, so you're getting more than just JSON back from the call (Firebug will show you that). As for your conclusion: using include/require by itself has absolutely no effect on the AJAX call (assuming there are no errors).
尝试更改:
至:
问题可能与
$return
在使用之前未声明为数组有关。编辑:好吧,所以这可能根本不是问题。但!可能发生的情况是您可能会在响应中重复某些内容。例如,如果在 JSON 之前回显错误,您将无法在客户端上解析它。
Try changing:
To:
The problem might have to do with
$return
not being declared as an array prior to use.Edit: Alright, so that might not be the problem at all. But! What might be happening is you might be echoing out something in the response. For example, if you have an error echoing out prior to the JSON, you'd be unable to parse it on the client.
如果“some.php”是一个空的 php 文件,为什么还需要它?!
如果 require 函数无法请求该文件,则会引发致命错误。尝试使用 include 函数来代替,看看会发生什么,如果它有效,那么您可能遇到了 require 'some.php' 的问题;
if the "some.php" is an empty php file, why do you require it at all?!
require function throws a fatal error if it could't require the file. try using include function instead and see what happens, if it works then you probably have a problem with require 'some.php';
require 调用不会有任何效果。您需要在 Firebug 中检查返回的输出。如果使用 Chrome,则有一个名为 Simple REST Client 的插件。 https://chrome.google.com/extensions/detail/fhjcajmcbmldlhcimfajhfbgofnpcjmb,您可以通过它快速查询东西。
此外,最好发送回正确的 HTTP 标头,并在响应中显示响应类型。
A require call won't have any effect. You need to check your returned output in Firebug. If using Chrome there is a plugin called Simple REST Client. https://chrome.google.com/extensions/detail/fhjcajmcbmldlhcimfajhfbgofnpcjmb through which you can quickly query for stuff.
Also, it's always good to send back proper HTTP headers with your response showing the response type.
正如上面所讨论的,它很可能是 BOM。我多次遇到同样的问题,并使用 Fiddler 检查十六进制文件,并注意到先前的备份和我创建的新文件中不存在额外的 3 个字节。不知何故,我的原始文件被 Textpad 修改了(都在 Windows 中)。虽然当我在 Notepad++ 中创建它们时我很好。
因此,如果您在 Windows 上进行开发并发布到托管提供商的 Linux 环境,请确保在创建、编辑和保存文件时正确设置编码和代码页,此外还要在操作系统之间保持一致。 。
It's most likely the BOM as has been discussed above. I had the same problem multiple times and used Fiddler to check the file in hex and noticed an extra 3 bytes that didn't exist in a prior backup and in new files I created. Somehow my original files were getting modified by Textpad (both in Windows). Although when I created them in Notepad++ I was fine.
So make sure that you have your encoding and codepages set up properly when you create, edit, and save your files in addition to keeping that consistent across OSes if you're developing on windows let's say and publishing to a linux environment at your hosting provider.