什么时候 404 不是 404?
我使用下面的 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);
}
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 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
也许 colorReset.php 正在创建 404,因为您缺少一些参数或者因为今天是星期三?要点是任何 php 脚本都可以出于编码人员想要的任何原因发出 404。该脚本在周三发送 404:
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:
因为根据 uffical 文档 $.get 等于此
页面说 http:// /api.jquery.com/jQuery.ajax/ that:
数字 HTTP 代码和响应具有相应代码时要调用的函数的映射。例如,当响应状态为 404 时,将发出以下警报:
如果请求成功,则状态码函数采用与成功回调相同的参数;如果导致错误,它们将采用与错误回调相同的参数。
Since according to uffical documentation $.get is equal to
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:
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
HTTP 404 来自响应的标头 - 可能有响应内容(通常是“哎呀,我们找不到它”消息),但如果它很小,它会被某些浏览器忽略(IE 对于小 404 有自己的消息) 。
我的猜测是服务器正在将 404 HTTP 状态标头添加到 colorReset.php - 这是 PHP/您正在使用的任何服务器的问题,而不是 jQuery 的问题。
如果您从服务器返回 HTTP 200 状态,jQuery 的
$.get
方法仅会触发success
函数,否则会触发error
函数 -因此您仍然可以获得 404 状态的颜色十六进制代码。更新
我认为这里有些混乱。
如果您在浏览器中访问 404 页面,它只会加载该页面的内容。
如果您通过
$.get
加载 404 页面,它将触发分配的error
方法,但$.get
上的构造函数仅允许您设置success
方法。如果你这样做,你的 jQuery 就会工作:
但是,我会看看为什么你的服务器首先返回 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 thesuccess
function if you get an HTTP 200 status back from the server, otherwise it fires theerror
function - so you could still get your colour hex code with the 404 status.Update
I think there's some confusion here.
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 assignederror
method, but the constructor on$.get
only lets you set thesuccess
method.Your jQuery would work if you did:
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.正如另一个答案中所述,加载 < 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.您可能想查看收到的响应的标头。发送带有 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).