jQuery Ajax 返回 404 错误,但响应正确 [英] jQuery Ajax returning 404 Error, but correct Response
问题描述
我正在通过 jQuery AJAX 将一些数据发布到 PHP 脚本,一切都正确执行,但它返回 404 错误.在我的 Firebug 控制台中,来自 PHP 脚本的响应是正确的.我不明白脚本如何响应,它仍然抛出 404 错误.jQueryerror"回调方法触发,success"方法不触发.
I'm posting some data to a PHP script via jQuery AJAX, and everything executes correctly, but it returns a 404 error. In my Firebug console the response from the PHP script is correct. I don't understand how the script can respond, and it is still throwing a 404 error. The jQuery "error" callback method triggers, and the "success" method doesn't.
PHP 脚本执行的所有语句都能准确工作,因为我可以看到正在更新的数据库等
All statements performed by the PHP script work accurately, because I can see the database being updated, etc.
我在 Dreamhost 托管的 WordPress 3.x 网站上使用 jQuery 1.4.2.
I'm using jQuery 1.4.2, on a WordPress 3.x website hosted by Dreamhost.
-----------更多信息-----------
好的,我发现当我在 Ajax 脚本中包含 WordPress 的 wp-blog-header.php
文件时,出现错误.此外,从前这些脚本可以工作,我 90% 确定它们在 WP 3.0 更新后停止工作.我将粘贴来自 Firebug 的响应标头.
OK, I've figured out that when I include WordPress's wp-blog-header.php
file in the Ajax script, I get the error. Also, once upon a time these scripts work, and I am 90% sure they stopped working after the WP 3.0 update. I'll paste in the Response headers from Firebug.
这个来自 PHP 的标头响应包含 wp-blog-header.php 并在 Firebug 中返回 404 错误...
Date Tue, 10 Aug 2010 01:44:44 GMT
Server Apache
X-Powered-By PHP/5.2.6
X-Pingback http://www.learnwake.com/xmlrpc.php
Expires Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control no-cache, must-revalidate, max-age=0
Pragma no-cache
Last-Modified Tue, 10 Aug 2010 01:44:44 GMT
Vary Accept-Encoding
Content-Encoding gzip
Content-Length 36
Keep-Alive timeout=2, max=98
Connection Keep-Alive
Content-Type text/html; charset=UTF-8
这个来自 PHP 的标头响应不包含 wp-blog-header.php 并在 Firebug 中返回 200 OK...
Date Tue, 10 Aug 2010 01:44:58 GMT
Server Apache
X-Powered-By PHP/5.2.6
Vary Accept-Encoding
Content-Encoding gzip
Content-Length 36
Keep-Alive timeout=2, max=100
Connection Keep-Alive
Content-Type text/html
推荐答案
当您包含 wp-blog-header.php
时,您最终会引导整个 WordPress 设置程序.函数wp()
被调用,它调用$wp->main()
,后者又调用各种设置函数.
When you include wp-blog-header.php
, you end up bootstrapping the whole WordPress setup routine. The function wp()
is called, which calls $wp->main()
, which in turn calls various setup functions.
其中之一是 $wp->query_posts()
,它调用 $wp_the_query->query()
,后者又调用 WP_Query
的 parse_query()
函数.我怀疑 404 指示是在那里生成的(您的 AJAX 页面不是 WP 帖子或类似的内容),然后由 $wp 转换为实际的 404 响应标头->handle_404()
,在main()
中query_posts()
之后调用的函数.
One of these is $wp->query_posts()
, which calls $wp_the_query->query()
, which in turn calls WP_Query
's parse_query()
function. I suspect that the 404 indication is generated in there (your AJAX page isn't a WP post, or anything like that), and is later transformed into an actual 404 response header by $wp->handle_404()
, the function called after query_posts()
in main()
.
我不是 100% 确定 parse_query()
是明确的罪魁祸首,但我建议您看看是否可以只包含 wp-load.php
,因为我相信它会完成创建您想要访问的对象的实际工作.
I'm not 100% sure that parse_query()
is the definite culprit, but I would suggest seeing if you can just include wp-load.php
instead, since I believe it does the actual work of creating the objects that you want to access.
再说一次,我实际上并没有使用 WordPress,所以我不能确定,但从我所知道的来看,这似乎是最有可能的情况.
Again, I don't actually use WordPress, so I can't be sure, but looking at the source code this seems to be the most likely case, from what I can tell.
这篇关于jQuery Ajax 返回 404 错误,但响应正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!