8

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);
}

Here is the javascript console result

I'm able to call the file directly using the same URL the console reports as a 404

Header Report

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($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;
?>
Scott B
  • 38,833
  • 65
  • 160
  • 266
  • 20
    That sure is a lot of tabs. – BoltClock Jun 01 '11 at 14:45
  • 2
    I figured I'd get razzed about the tabs. I'm a browser "pack rat" I suppose :-) – Scott B Jun 01 '11 at 14:47
  • 2
    You might want to see if your situation is similar to [this one](http://stackoverflow.com/questions/3445270/jquery-ajax-returning-404-error-but-correct-response). – Tim Stone Jun 01 '11 at 14:48
  • why the adress so secret? you should compare the two address. btw `$.get('...` this looks really ugly. you shouldn't mix javascript with php like this. – Gergely Fehérvári Jun 01 '11 at 14:49
  • Had to black out the address to respect privacy of the owner (not me, I'm working to help resolve the issue). – Scott B Jun 01 '11 at 14:59
  • 2
    @Jamie: chrome has some pretty damn good developer tools built into it. hit `ctrl-shift-i` to bring them up. – Marc B Jun 01 '11 at 15:06
  • Thanks for the headers info - your problem lies with Apache 2.2.19 and PHP, not jQuery. Can we see the source of `colorReset.php`? – Keith Jun 01 '11 at 15:09
  • @Keith, thanks. I just updated with the source of colorReset.php – Scott B Jun 01 '11 at 15:16

6 Answers6

13

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.

Community
  • 1
  • 1
Tim Stone
  • 19,119
  • 6
  • 56
  • 66
6

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

Waldheinz
  • 10,399
  • 3
  • 31
  • 61
  • 1
    Servers **should** send a document with a 404 error — it should explain that the page was not found, and possibly provide a course of action ("I couldn't find /foo, but I know about /food, which is similar, did you mean that? Here is a link"). Sometimes a server will be so horribly misconfigured that it sends 404 for everything. – Quentin Jun 01 '11 at 14:49
  • Thanks Waldheinz. The header's say the same thing: Status Code:404 Not Found – Scott B Jun 01 '11 at 14:50
  • I'm not really complaining about the fact that the browser is reporting 404. Just trying to determine why, so I can fix it. Although it seems odd that I can execute the exact same URL outside of the jQuery get and not get a 404, so there's something in the jQuery get that's causing the 404, right? – Scott B Jun 01 '11 at 14:56
  • 2
    @Scott B - it's not jQuery `$.get` that's causing the 404 - its the web server that's returning it. The browser will still display a 404 page if there is response content. However, the `$.get` fires the `error` method instead of the `success` one and you've only supplied the `success` method. – Keith Jun 01 '11 at 15:06
  • 1
    @scott: check the server's access log and compare the hits. There must be some difference between the jquery version and your "do it manually" hit. – Marc B Jun 01 '11 at 15:07
4

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.

Keith
  • 150,284
  • 78
  • 298
  • 434
  • Very helpful Keith. I think you've gotten me over the hump with this one. – Scott B Jun 01 '11 at 15:23
  • Keith, you're right on track. I've removed some of the calls in the php and its no longer returning 404. I'm isolated on colorReset.php now to resolve. Thanks for diving in! – Scott B Jun 01 '11 at 15:46
  • @Scott B - H2H. Take a look at @Tim Stone's answer too - I think he's right: `require_once('../../../wp-blog-header.php');` finds that `colorReset.php` isn't part of WordPress and so sets the HTTP status header to 404. – Keith Jun 01 '11 at 16:00
  • thanks for pointing that out. Not sure why I didn't see his original comment, but its spot-on to the cause of the problem. – Scott B Jun 02 '11 at 01:18
2

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.

max4ever
  • 11,909
  • 13
  • 77
  • 115
0

this file is in same directory?

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

Pratik
  • 30,639
  • 18
  • 84
  • 159
0

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");
}
James
  • 20,957
  • 5
  • 26
  • 41