jQuery Ajax返回404错误,但响应正确

时间:2022-10-08 09:07:17

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.

我正在通过jQuery AJAX向PHP脚本发布一些数据,并且所有内容都执行正确,但是它返回一个404错误。在我的Firebug控制台,PHP脚本的响应是正确的。我不知道脚本如何响应,它仍然抛出404错误。jQuery“错误”回调方法触发,“成功”方法不触发。

All statements performed by the PHP script work accurately, because I can see the database being updated, etc.

PHP脚本执行的所有语句都可以准确地工作,因为我可以看到正在更新的数据库等等。

I'm using jQuery 1.4.2, on a WordPress 3.x website hosted by Dreamhost.

我在WordPress 3上使用jQuery 1.4.2。x网站由Dreamhost主办。

-----------MORE INFO-----------

- - - - - - - - - - - - - - - - - - - - - - -——信息

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.

当我把WordPress的wp-blog-header包含进去的时候,我就知道了。在Ajax脚本中的php文件中,我得到了错误。此外,这些脚本曾经有效,我90%的确信它们在WP 3.0更新后停止工作。我将粘贴来自Firebug的响应头。

This header response from PHP that includes the wp-blog-header.php and returns a 404 error in Firebug...

这个来自PHP的报头响应包含了wp-blog报头。在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

This header response from PHP that doesn't include the wp-blog-header.php and returns a 200 OK in Firebug...

这个来自PHP的报头响应不包括wp-blog-header。在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

6 个解决方案

#1


47  

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-blog-header。php,你最终会启动整个WordPress的设置程序。调用函数wp(),它调用$wp->main(),后者反过来调用各种设置函数。

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().

其中一个是$wp->query_posts(),它调用$wp_the_query->查询(),它又调用WP_Query的parse_query()函数。我怀疑404指示是在那里生成的(您的AJAX页面不是WP post,或者类似的东西),然后被$ WP ->handle_404()转换为实际的404响应头,该函数在main()中名为query_posts()。

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.

我不能100%肯定parse_query()是罪魁祸首,但我建议您看看是否可以只包含wpar -load。而是php,因为我相信它实际做的工作是创建您想要访问的对象。

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.

再说一遍,我实际上没有使用WordPress,所以我不能确定,但是从源代码来看,这似乎是最有可能的情况,从我所知道的情况来看。

#2


3  

I've added an ajax.php file in a WordPress template once, and had this problem.

我添加了一个ajax。一个WordPress模板中的php文件,并且有这个问题。

I solved it simply by adding at the top of ajax.php

我通过在ajax.php的顶部添加来解决这个问题

header('Response: HTTP/1.1 200 OK');

Kind of a hack, but it worked.

有点像黑客,但很管用。

#3


3  

No one else posted this as an answer, so it's worth noting. You should be including wp-load.php instead of wp-blog-header.php.

没有人贴出这个作为答案,所以这是值得注意的。你应该包括wp-load。代替wp-blog-header.php php。

If you open up wp-blog-header.php you'll see why:

如果你打开wp-blog-header。php你就会明白为什么:

if ( !isset($wp_did_header) ) {

    $wp_did_header = true;

    require_once( dirname(__FILE__) . '/wp-load.php' );

    wp();

    require_once( ABSPATH . WPINC . '/template-loader.php' );

}

If you are only outputting json for an AJAX operation, you do not need to include template-loader.php. This will create unnecessary overhead, and then of course provide the 404 error.

如果您仅为AJAX操作输出json,则不需要包括template-loader.php。这将产生不必要的开销,然后当然会提供404错误。

This 'workaround' is necessary for current and future versions of WordPress. I'm assuming anything past 3.0 should include wp-load.php as stated.

这种“变通”对于WordPress的当前和未来版本都是必要的。我假设任何超过3.0的东西都应该包括wp负载。php如上所述。

#4


1  

Overall there aren't a ton of places where WordPress will return a 404. I recommending grepping the source tree for those places and placing some debug code to trace why it's happening.

总的来说,WordPress返回404页面的地方并不多。我建议为这些地方添加源代码树,并放置一些调试代码来跟踪它发生的原因。

#5


0  

Based on the answer from Tim, I changed the hook I was catching from "wp" to "init" in my plugin and it stopped giving me the 404.

基于Tim的回答,我换了一个钩子,从“wp”到“init”在我的插件中,它不再给我404。

#6


0  

I had the same problem.

我也有同样的问题。

The Fix.

修复。

Change:

变化:

require_once('wp-blog-header.php');

To:

:

require_once('conn.php');
require('wp-config.php');
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();

This will also fix HTTP header errors if you want to have a page outside WP.

如果您希望在WP之外有一个页面,这也将修复HTTP头错误。

#1


47  

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-blog-header。php,你最终会启动整个WordPress的设置程序。调用函数wp(),它调用$wp->main(),后者反过来调用各种设置函数。

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().

其中一个是$wp->query_posts(),它调用$wp_the_query->查询(),它又调用WP_Query的parse_query()函数。我怀疑404指示是在那里生成的(您的AJAX页面不是WP post,或者类似的东西),然后被$ WP ->handle_404()转换为实际的404响应头,该函数在main()中名为query_posts()。

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.

我不能100%肯定parse_query()是罪魁祸首,但我建议您看看是否可以只包含wpar -load。而是php,因为我相信它实际做的工作是创建您想要访问的对象。

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.

再说一遍,我实际上没有使用WordPress,所以我不能确定,但是从源代码来看,这似乎是最有可能的情况,从我所知道的情况来看。

#2


3  

I've added an ajax.php file in a WordPress template once, and had this problem.

我添加了一个ajax。一个WordPress模板中的php文件,并且有这个问题。

I solved it simply by adding at the top of ajax.php

我通过在ajax.php的顶部添加来解决这个问题

header('Response: HTTP/1.1 200 OK');

Kind of a hack, but it worked.

有点像黑客,但很管用。

#3


3  

No one else posted this as an answer, so it's worth noting. You should be including wp-load.php instead of wp-blog-header.php.

没有人贴出这个作为答案,所以这是值得注意的。你应该包括wp-load。代替wp-blog-header.php php。

If you open up wp-blog-header.php you'll see why:

如果你打开wp-blog-header。php你就会明白为什么:

if ( !isset($wp_did_header) ) {

    $wp_did_header = true;

    require_once( dirname(__FILE__) . '/wp-load.php' );

    wp();

    require_once( ABSPATH . WPINC . '/template-loader.php' );

}

If you are only outputting json for an AJAX operation, you do not need to include template-loader.php. This will create unnecessary overhead, and then of course provide the 404 error.

如果您仅为AJAX操作输出json,则不需要包括template-loader.php。这将产生不必要的开销,然后当然会提供404错误。

This 'workaround' is necessary for current and future versions of WordPress. I'm assuming anything past 3.0 should include wp-load.php as stated.

这种“变通”对于WordPress的当前和未来版本都是必要的。我假设任何超过3.0的东西都应该包括wp负载。php如上所述。

#4


1  

Overall there aren't a ton of places where WordPress will return a 404. I recommending grepping the source tree for those places and placing some debug code to trace why it's happening.

总的来说,WordPress返回404页面的地方并不多。我建议为这些地方添加源代码树,并放置一些调试代码来跟踪它发生的原因。

#5


0  

Based on the answer from Tim, I changed the hook I was catching from "wp" to "init" in my plugin and it stopped giving me the 404.

基于Tim的回答,我换了一个钩子,从“wp”到“init”在我的插件中,它不再给我404。

#6


0  

I had the same problem.

我也有同样的问题。

The Fix.

修复。

Change:

变化:

require_once('wp-blog-header.php');

To:

:

require_once('conn.php');
require('wp-config.php');
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();

This will also fix HTTP header errors if you want to have a page outside WP.

如果您希望在WP之外有一个页面,这也将修复HTTP头错误。