什么时候 404 不是 404?

发布于 2024-11-10 18:26:24 字数 2273 浏览 1 评论 0原文

我使用下面的 jQuery 调用来加载位于同一服务器上的 .php 文件。

然而,使用 Chrome 的 javascript 控制台,它在我尝试加载的 php 文件上报告“404 not found”。不过,我可以直接加载该文件,只需从控制台中单击该文件即可。

另外,我可以直接从报告 404(未找到)的 javascript 控制台复制文件的 URL,打开一个新选项卡,将其粘贴到地址栏中,然后正常点击脚本,没有问题。

这是 jQuery get 方法特有的东西吗?是什么原因导致 get 方法中出现页面 404,但直接调用时执行正常?

$('.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);
}

这是 javascript 控制台结果

我可以使用控制台报告为 404 的相同 URL 直接调用该文件

Header Report

Here是源文件 colorReset.php,由 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;
?>

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;
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

薄荷→糖丶微凉 2024-11-17 18:26:35

也许 colorReset.php 正在创建 404,因为您缺少一些参数或者因为今天是星期三?要点是任何 php 脚本都可以出于编码人员想要的任何原因发出 404。该脚本在周三发送 404:

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

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");
}
栀子花开つ 2024-11-17 18:26:34

因为根据 uffical 文档 $.get 等于此

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

页面说 http:// /api.jquery.com/jQuery.ajax/ that:

数字 HTTP 代码和响应具有相应代码时要调用的函数的映射。例如,当响应状态为 404 时,将发出以下警报:

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

如果请求成功,则状态码函数采用与成功回调相同的参数;如果导致错误,它们将采用与错误回调相同的参数。

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.

深空失忆 2024-11-17 18:26:34

该文件位于同一目录中吗?

如果该文件位于同一目录中,则无需提供反斜杠,只需提供文件名

this file is in same directory?

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

一身骄傲 2024-11-17 18:26:33

HTTP 404 来自响应的标头 - 可能有响应内容(通常是“哎呀,我们找不到它”消息),但如果它很小,它会被某些浏览器忽略(IE 对于小 404 有自己的消息) 。

我的猜测是服务器正在将 404 HTTP 状态标头添加到 colorReset.php - 这是 PHP/您正在使用的任何服务器的问题,而不是 jQuery 的问题。

如果您从服务器返回 HTTP 200 状态,jQuery 的 $.get 方法仅会触发 success 函数,否则会触发 error 函数 -因此您仍然可以获得 404 状态的颜色十六进制代码。


更新

我认为这里有些混乱。

  • HTTP 404 并不意味着您的浏览器找不到该页面
  • HTTP 404 意味着服务器(在本例中为 Apache)告诉您它找不到该页面,但它仍然返回包含内容的页面。

如果您在浏览器中访问 404 页面,它只会加载该页面的内容。

如果您通过 $.get 加载 404 页面,它将触发分配的 error 方法,但 $.get 上的构造函数仅允许您设置success 方法。

如果你这样做,你的 jQuery 就会工作:

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

但是,我会看看为什么你的服务器首先返回 404 - colorReset.php 可能有错误,或者服务器配置可能是错误的。

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.

我做我的改变 2024-11-17 18:26:32

正如另一个答案中所述,加载 < code>wp-blog-header.php 引导整个 WordPress 请求处理过程。鉴于您的脚本实际上不是 WordPress 帖子,此过程会设置 404 标头以表明它找不到您要查找的内容。

由于看起来您真正想要的只是访问 WordPress 用户函数,因此您最好只包含 wp-load.php ,它应该允许您在不调用请求解析器的情况下调用这些函数。

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.

陈独秀 2024-11-17 18:26:32

您可能想查看收到的响应的标头。发送带有 404 响应的文档并不罕见,浏览器可能会显示该响应。但它仍然是 404 响应,JQuery 会将其视为错误(根据 HTTP 标准,它实际上是错误)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文