Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

jQuery Ajax returning 404 Error, but correct Response

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.

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

I'm using jQuery 1.4.2, on a WordPress 3.x website hosted by 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.

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

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...