jQuery Ajax returning 404 Error, but correct Response

后端 未结 6 1524
既然无缘
既然无缘 2020-12-01 18:07

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 scrip

相关标签:
6条回答
  • 2020-12-01 18:17

    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.

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

    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.

    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.

    0 讨论(0)
  • 2020-12-01 18:26

    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.

    0 讨论(0)
  • 2020-12-01 18:28

    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.

    0 讨论(0)
  • 2020-12-01 18:33

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

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

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

    Kind of a hack, but it worked.

    0 讨论(0)
  • 2020-12-01 18:36

    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.

    0 讨论(0)
  • 2020-12-01 18:39

    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.

    0 讨论(0)
提交回复
热议问题