开发者

When is a 404 not a 404?

I'm using this jQuery below call to load a .php file located on the same server.

However, using Chrome's javascript console, its reporting "404 not found" on the php file I'm trying to load. Although, I can load the file directly, just by clicking on the file right there from within the console.

Also, I can copy the URL of the file, right from the javascript console where it reports 404 (not found), open a new tab, paste it into the address bar, and hit the script fine, without issue.

Is this something specific to the jQuery get method? What could be causing the page to 404 in the get method but execute fine when called directly?

$('.colorReset').click
    (
        function() 
        {
        var myImage = $('#theme :selected').text();
        $.get('<?php echo get_bloginfo('template_directory') ?>/colorReset.php', {theme: myImage, spot: '1'}, function(data){doColor('#theme_header_color', data);});
        }
    );

    //script never gets to the doColor function, due to the apparent 404 on colorReset.php
function doColor(el, color)
    {
    $(el).val(color).trigger('keyup');
    $(el).attr('value', color);
    $(el).val(color);
}

When is a 404 not a 404?

When is a 404 not a 404?

When is a 404 not a 404?

Here is the source file, colorReset.php, that's called by the get...

<?php
require_once('../../../wp-blog-header.php');

add_action( 'admin_init', 'check_user' );

function check_user()
    {
    if (!is_user_logged_in()){
        die("You Must Be Logged In to Access This");
    }
    if( ! current_user_can('edit_files')) {
        die("Oops sorry you are not authorized to do this");
    }
}

$myTheme = $_REQUEST['theme'];
$spot = $_REQUEST['spot'];
$myThemeColor = $myTheme."_color".$spot;

$file = "styles/".$myTheme."/template.ini";
    if (file_exists($file) && is_readable($file))
    {
    $ini_array = parse_ini_file($file);
     if($spot == 1){$myColor = $ini_array['color1'];}
     if($spot == 2){$myColor = $ini_array['color2'];}
     if($spot == 3){$myColor = $ini_array['color3'];}
     if开发者_JAVA百科($spot == 4){$myColor = $ini_array['color4'];}
    }
    else
    {
     if($spot == 1){$myColor = get_option('theme_header_color');}
     if($spot == 2){$myColor = get_option('theme_sidebar_color');}
     if($spot == 3){$myColor = get_option('theme_spot_color_alt');}
     if($spot == 4){$myColor = get_option('theme_spot_color_alt2');}
    }
echo $myColor;
?>


As described in another answer, loading wp-blog-header.php bootstraps the entire WordPress request handling process. Given that your script isn't actually a WordPress post, this process sets the 404 header to indicate that it couldn't find the content you were looking for.

Since it looks like what you really want is just access to the WordPress user functions, you're better off just including wp-load.php which should allow you to call those functions without invoking the request parser.


You might want to look at the headers of the response you receive. It is not uncommon to send some document with a 404 response, which a browser might display. But it's still a 404 response, and JQuery will treat it like an error (which, by the HTTP standard it actually is).


The HTTP 404 comes from the header on the response - there can be response content (usually an "Oops we can't find it" message) but it gets ignored by some browsers if it's small (IE does it's own messages for small 404s).

My guess would be that the server is adding the 404 HTTP status header to colorReset.php - this is a PHP/whatever server you're using's issue, not jQuery's.

jQuery's $.get method only fires the success function if you get an HTTP 200 status back from the server, otherwise it fires the error function - so you could still get your colour hex code with the 404 status.


Update

I think there's some confusion here.

  • An HTTP 404 does not mean that your browser cannot find the page
  • An HTTP 404 means that the server (in this case Apache) is telling you that it couldn't find the page, but it's still returning a page with content.

If you visit a 404 page in your browser it will just load the page's content.

If you load a 404 page via $.get it will fire the assigned error method, but the constructor on $.get only lets you set the success method.

Your jQuery would work if you did:

var myImage = $('#theme :selected').text();

$.ajax({
    url:     '<?php echo get_bloginfo('template_directory') ?>/colorReset.php',
    data:    {theme: myImage, spot: '1'},
    success: function(data){doColor('#theme_header_color', data);},
    error:   function(data){doColor('#theme_header_color', data);}
});

However, I'd look at why your server is returning a 404 first - colorReset.php may have a bug in it or the server configuration may be wrong.


Since according to uffical documentation $.get is equal to

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

this page says http://api.jquery.com/jQuery.ajax/ that:

A map of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:

$.ajax({
  statusCode: {
    404: function() {
      alert('page not found');
    }
  }
});

If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback.


this file is in same directory?

if this file is in same dir then no need to give backslash just give the file name


Maybe colorReset.php is creating the 404 because you are missing some parameters or because it's Wednesday? The point being that any php script can issue a 404 for any reason that the coder desires. This script sends 404 on Wednesdays:

$weekday = date('l');
if ($weekday == 'Wednesday') {
  header("HTTP/1.0 404 Not Found");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜