javascript 从javascript获取当前页面http状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14654390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current page http status from javascript
提问by Andrea Zilio
Is there any way to get the http status of the current web page from javascript?
有没有办法从javascript获取当前网页的http状态?
Spent some time searching on the web, but no luck at all... Seems like it's not possible, but wanted to check with Stack Overflow for maybe some fancy workaround.
花了一些时间在网上搜索,但根本没有运气......似乎不可能,但想检查 Stack Overflow 上也许有一些奇特的解决方法。
(Providing it from the server as part of the response body is not acceptable, the status is supposed to be only available via the http header)
(从服务器提供它作为响应正文的一部分是不可接受的,状态应该只能通过 http 标头获得)
采纳答案by Martin Jespersen
This is not in any way possible, sorry.
这是不可能的,抱歉。
回答by John
Yes, if you control the server-side scripting.
是的,如果您控制服务器端脚本。
I just encountered a scenario where I do notwant my platform's Image Manager to load (the page's content is HTTP 404) for a project I'm working on. I only have to reference the JSON data loaded with each page load (Web 1.0 or Web 3.0 requests); I only have to use the following:
我刚刚遇到了一个场景,我不希望我的平台的图像管理器为我正在处理的项目加载(页面的内容是 HTTP 404)。我只需要引用每次页面加载时加载的 JSON 数据(Web 1.0 或 Web 3.0 请求);我只需要使用以下内容:
if (JSON.parse(id_(push_current_id()+'_json').textContent).http == 200) {}
Howevermost people are not lucky enough to either have the time to build their own system or dig through the wildly convoluted code of whatever they're stuck using. In those scenarios if you're using PHP then you could have a look at PHP's headers_list.
然而,大多数人都没有足够的时间来构建他们自己的系统或挖掘他们一直在使用的任何复杂的代码。在这些情况下,如果您使用 PHP,那么您可以查看 PHP 的headers_list。
If the second option isn't viable then the next approach would be to determine if your CMS/CRM is competent enough that it determines if the requested URL is HTTP 200, 404, 500, etc. Otherwise if you're using their software to power a business and a competitor is a crony they can just link to thousands of non-existent pages, your system would return HTTP 200 for all of them and then you'd take a considerable hit on search engines. So in this scenario I'd find out how to access your software's HTTP and then use that to output to the JavaScript Number object.
如果第二个选项不可行,那么下一个方法是确定您的 CMS/CRM 是否足够胜任,以确定请求的 URL 是否为 HTTP 200、404、500 等。否则,如果您使用他们的软件来为企业提供动力,而竞争对手是亲信,他们只能链接到数千个不存在的页面,您的系统将为所有这些页面返回 HTTP 200,然后您将在搜索引擎上受到相当大的打击。因此,在这种情况下,我将了解如何访问您软件的 HTTP,然后使用它来输出到 JavaScript Number 对象。
JavaScript AJAX HTTP Status
JavaScript AJAX HTTP 状态
var xhr = new XMLHttpRequest();
xhr.open(method,url,true);
xhr.send(null);
xhr.onreadystatechange = function()
{
if (xhr.readyState=='4') {console.log(xhr.status);}
}
If you're not using my platform or a decent CMS/CRM the only thing left is if you can get away with relying on if an AJAX request is what you are depending on however (at least from my perspective) you're likely relying on the Web 1.0 request's HTTP status in order to make a Web 2.0 request. Most systems, even half decent ones are going to have a lot of inconsistency because software is like a child and most software doesn't have two consistent original parents. At this point you're forced to reevaluate: if this problem worth my time? If so then do I pointlessly continue to use this software or dedicate myself to looking at alternatives?
如果您没有使用我的平台或不错的 CMS/CRM,那么剩下的唯一事情就是您是否可以依赖 AJAX 请求是否是您所依赖的,但是(至少从我的角度来看)您可能会依赖Web 1.0 请求的 HTTP 状态,以便发出 Web 2.0 请求。大多数系统,即使是半体面的系统也会有很多不一致,因为软件就像一个孩子,大多数软件没有两个一致的原始父母。此时你不得不重新评估:这个问题是否值得我花时间?如果是这样,那么我是毫无意义地继续使用该软件还是致力于寻找替代方案?
Lastly for those willing to make the fulleffort: advocate the standards bodies to implement a JavaScript API. It shouldbe dead simple however as someone who is constantly bumping up against the apexes of the current available capabilities of web software I recommend tempering one's expectations: there are enough incompetent people in the standards bodies (there are a lot of very competent and passionate people too) who are in leadership/decision-making positions that you will hit political red tape. I'm not saying it won't happen though for even for something so simple do not expect a miracle.
最后对于那些愿意做出充分努力:倡导标准机构来实现的JavaScript API。这应该很简单,但是作为一个不断与当前可用的网络软件功能的顶点发生冲突的人,我建议调整自己的期望:标准机构中有足够多的不称职的人(有很多非常称职和热情的人)太)那些担任领导/决策职位的人,你会遇到上的繁文缛节。我并不是说它不会发生,即使对于如此简单的事情也不要指望奇迹。
Best of luck!
祝你好运!
回答by saeed arab sheybani
you can only check status of page loading try:var x = document.readyState; The result of x could be: One of five values:
你只能检查页面加载的状态 try:var x = document.readyState; x 的结果可能是: 五个值之一:
uninitialized - Has not started loading yet
loading - Is loading
loaded - Has been loaded
interactive - Has loaded enough and the user can interact with it
complete - Fully loaded
回答by Danilo Soncini
It is not beautiful, but you can use:
它并不漂亮,但您可以使用:
t = jQuery.get(location.href)
.success(function () { console.log(t.status) })
.error(function() { console.log(t.status) });