When is 404 not 404?

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

However, using the Chrome javascript console, its report “404 not found” in the php file I'm trying to download. Although, I can upload the file directly by simply clicking the file right there from the console.

In addition, I can copy the file URL directly from the javascript console where it reports 404 (not found), open a new tab, paste it into the address bar and click on the script icon, no problem.

Is this something specific to the jQuery get method? What can cause page 404 in the get method, but execute a penalty 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 colorReset.php file called by 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; ?> 
+8
jquery php apache
source share
6 answers

As described in another answer , loading wp-blog-header.php loads the entire WordPress request processing process. Given that your script is not really a WordPress post, this process sets the 404 header to indicate that it could not find the content you were looking for.

Since it seems to you that you really want to access WordPress custom functions, you are best off with wp-load.php , which should allow you to call these functions without calling a request parser.

+13
source share

You might want to see the headers of the response you will receive. It’s not uncommon to send a document with a 404 response that the browser can display. But this is still 404's answer, and JQuery will treat it as an error (which, by the HTTP standard, is actually there).

+6
source share

HTTP 404 comes from the response header - there may be response content (usually the message "Oops, we cannot find it"), but it is ignored by some browsers if it is small (IE has its own messages for small 404s).

I assume the server adds an HTTP 404 status header to colorReset.php - this is PHP / any server you are using the problem on, not jQuery.

The jQuery $.get method only runs the success function if you return the HTTP 200 status from the server, otherwise it runs the error function, so you can still get your hex color code with the status 404.


Update

I think there is some kind of confusion.

  • HTTP 404 does not mean that your browser cannot find the page
  • HTTP 404 means that the server (in this case, Apache) tells you that it cannot find the page, but still returns a page with the content.

If you go to page 404 in your browser, it will just load the contents of the page.

If you load the 404 page via $.get , it will miss the assigned error method, but the constructor on $.get only allows you to set the success method.

Your jQuery will work if you do this:

 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 would see why your server returns 404 first - colorReset.php may have an error or the server configuration may be incorrect.

+4
source share

Since according to uffical documentation $. get equals

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

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

A map of the numeric codes and HTTP functions that will be called when the response has the appropriate code. For example, the following will warn when the response status is 404:

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

If the request is successful, the status code functions have the same parameters as the success callback; if this leads to an error, they accept the same parameters as the error callback.

+2
source share

Is this file in the same directory?

if this file is in the same directory, then there is no need to give a backslash, just specify the file name

0
source share

Maybe colorReset.php creates 404 because you are missing some parameters or because of its environment? The fact is that any php script can produce 404 for any reason that the encoder wants. This script sends 404 on Wednesdays:

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

All Articles