是否为0的HTTP状态code有什么意义呢? [英] Does an HTTP Status code of 0 have any meaning?

查看:5377
本文介绍了是否为0的HTTP状态code有什么意义呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,当你从一个脚本中XMLHtt prequest在浏览器中,如果浏览器被设置为脱机工作或者网线被拔出,请求一个错误和状态= 0完成0没有列为允许HTTP状态codeS。

这是什么状态0 code是什么意思?这是否意味着在所有浏览器同样的事情,并为所有的HTTP客户端事业吗?它是HTTP规范的一部分,或者是一些其他的协议规范的一部分?这似乎意味着,在HTTP请求不能在所有的提出,也许是因为服务器地址无法得到解决。

什么错误信息是适当的显示给用户? 要么你没有连接到互联网,或者该网站遇到问题,或有可能是在地址输入错误?

我要补充一点,我看的时候,当设置为脱机工作设置为脱机工作,而不是在Microsoft Internet Explorer在Firefox的行为。在IE浏览器中,用户将获得一个对话框,让选择到网上去。返回错误前,Firefox不会通知用户。

我在回应询问这一个请求表现出更好的错误信息。什么Internet Explorer中确实还是不错的。它告诉是什么原因造成的问题,并让他们来解决它的选项的用户。为了给等效UX与Firefox我需要来推断问题的原因,并告知用户。因此,在总的我可以从状态0推断?它有一个普遍的意义或没有告诉我什么?

解决方案

简答

这是的没有的HTTP响应code,但它的的通过W3记录为状态的一个有效的值属性的 XMLHtt prequest (因而也是一个 jqXHR 的对象,jQuery的用户)

它涵盖了可能出现的情况整体裹在其中有没有真正的HTTP响应code可用于报告,或者是因为你还没有发出的请求,您明确放弃它,页面被卸载,或 X 想歪了的 X

许多可能值中的一个

长的答案

首先,重申:0的没有的一个HTTP状态code。有他们在 RFC 7231第6.1节前奏的完整列表,不包括0,第6条明确指出,

  

该岗位code元素是一个三位数的整数code

而0是没有的。

不过,0为状态值 XMLHtt prequest 对象的属性的的记录。从在<一个文档href="http://www.w3.org/TR/XMLHtt$p$pquest/#the-status-attribute">http://www.w3.org/TR/XMLHtt$p$pquest/#the-status-attribute:

  

4.7.1状态属性

     

地位属性必须返回运行这些步骤的结果是:

     
      
  1. 如果状态未发送或打开,返回0。

  2.   
  3. 如果错误标志设置,返回0。

  4.   
  5. 返回HTTP状态code。

  6.   

我们可以深入挖掘的规范,并找出正是这些条件,返回0的意思。从<一个href="http://www.w3.org/TR/XMLHtt$p$pquest/#states">http://www.w3.org/TR/XMLHtt$p$pquest/#states:

  

4.5各国

     

...

     

UNSENT (数值0)

     

该对象已建成。

     

开放(数值1)

     

的open()方法已成功地调用。在这种状态下请求头可以使用设置 setRequestHeader()和请求可以使用发送()的方法进行

     

...

     

错误标记表示某种类型的网络错误或提取终止。它最初是没有设置。

这也是必要指出, UNSENT 开放

  

HEADERS_RECEIVED (数值2)

     

所有重定向(如果有的话)进行了跟踪,并已收到最终响应的所有HTTP头。现已对象的几个答复成员。

把这个一起,简单的答案是,0仅仅是将要由状态返回的属性 XMLHtt prequest 对象时,有没有真正的状态code返回,因为无论:

  • 的请求尚未被发送,或者
  • 的请求已发送但响应的报头还没有被接收到,或者
  • 在一个许多可能出现的情况时有发生,列在的文档,已经造成了错误标志被设置。

好了,但什么错误可能导致这个神秘的错误标志被设置?如果你CTRL-F为W3文档中的错误标志,你会发现,它得到的取消设置的根据发送请求,它永远只能得到的设置的作为的一部分该算法为终止该请求。寻找所有的地方的的算法被调用时,你会发现,它发生的时候:

  • 在该请求被打开(或重新打开)与的open()方法
  • 在该请求被垃圾回收(如离开页面的时候)
  • 在该请求被中止与中止()方法
  • 一个请求错误发生,可在下列情形之一发生:

    • 一个网络错误发生时,如果

      它可以发生
      • 在这里有一个无限重定向循环
      • 在有/有
          

        DNS错误,TLS协商失败,或其他类型的网络错误

      • 在该请求是CORS请求和响应不能共享
    • 这是中止发生错误,这只能发生,如果

        

      最终用户取消请求

      是什么意思。我不知道,显示了当AJAX请求正在发生的用户,并让他们有机会以明确取消他们的任何浏览器,所以我觉得这个人是 - 至少在今天 - 无关

    • 一个超时错误发生时,这意味着,有理由的。

        

      暂停 不为0,自请求启动由指定的毫秒数暂停已通过

XMLHtt prequest 去,这就是一切。

除了 XMLHtt prequest ,我将很可能使用的是0状态code同样,作为一个默认的推测,HTTP库的语言JavaScript的外当没有状态code已经收到来自服务器的值。

It appears that when you make an XMLHttpRequest from a script in a browser, if the browser is set to work offline or if the network cable is pulled out, the request completes with an error and with status = 0. 0 is not listed among permissible HTTP status codes.

What does a status code of 0 mean? Does it mean the same thing across all browsers, and for all HTTP client utilities? Is it part of the HTTP spec or is it part of some other protocol spec? It seems to mean that the HTTP request could not be made at all, perhaps because the server address could not be resolved.

What error message is appropriate to show the user? "Either you are not connected to the internet, or the website is encountering problems, or there might be a typing error in the address"?

I should add to this that I see the behavior in FireFox when set to "Work Offline", but not in Microsoft Internet Explorer when set to "Work Offline". In IE, the user gets a dialog giving the option to go online. FireFox does not notify the user before returning the error.

I am asking this in response to a request to "show a better error message". What Internet Explorer does is good. It tells the user what is causing the problem and gives them the option to fix it. In order to give an equivalent UX with FireFox I need to infer the cause of the problem and inform the user. So what in total can I infer from Status 0? Does it have a universal meaning or does it tell me nothing?

解决方案

Short Answer

It's not a HTTP response code, but it is documented by W3 as a valid value for the status attribute of an XMLHttpRequest (and thus also of a jqXHR object, for jQuery users).

It covers a whole swathe of possible situations in which there's no real HTTP response code available to report, either because you haven't sent the request, you explicitly aborted it, the page is unloading, or x went wrong for one of many possible values of x.

Long Answer

First, to reiterate: 0 is not a HTTP status code. There's a complete list of them in RFC 7231 Section 6.1, that doesn't include 0, and the intro to section 6 states clearly that

The status-code element is a three-digit integer code

which 0 is not.

However, 0 as a value of the status attribute of an XMLHttpRequest object is documented. From documentation at http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute:

4.7.1 The status attribute

The status attribute must return the result of running these steps:

  1. If the state is UNSENT or OPENED, return 0.

  2. If the error flag is set, return 0.

  3. Return the HTTP status code.

We can dig deeper into the spec and find out just what those conditions for returning 0 mean. From http://www.w3.org/TR/XMLHttpRequest/#states:

4.5 States

...

UNSENT (numeric value 0)

The object has been constructed.

OPENED (numeric value 1)

The open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method.

...

The error flag indicates some type of network error or fetch termination. It is initially unset.

It's also relevant to note that the next possible state after UNSENT and OPENED is HEADERS_RECEIVED:

HEADERS_RECEIVED (numeric value 2)

All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available.

Putting this all together, the short answer is that 0 is simply what gets returned by the status attribute of an XMLHttpRequest object when there is no real status code to return, because either:

  • The request hasn't yet been sent, or
  • The request has been sent but the headers of the response have not yet been received, or
  • One of many possible circumstances have occurred, listed in the docs, that have caused the "error flag" to be set.

Okay, but what errors can cause this mysterious "error flag" to be set? If you CTRL-F for 'error flag' in the W3 documentation, you will find that it gets unset upon sending the request, and it only ever gets set as part of the algorithm to "terminate the request". Looking for all the places that algorithm is invoked, you'll find that it happens when:

  • The request is opened (or re-opened) with the open() method
  • The request is garbage collected (e.g. when leaving the page)
  • The request is aborted with the abort() method
  • A 'request error' happens, which can happen when one of the following situations occurs:

    • A network error occurs, which can happen if

      • There's an infinite redirect loop
      • There is/are

        DNS errors, TLS negotiation failure, or other type of network error

      • The request was a CORS request, and the response cannot be shared
    • An abort error occurs, which can only happen if

      The end user cancels the request

      whatever that means. I don't know of any browser that shows users when AJAX requests are occurring and gives them the opportunity to cancel them explicitly, so I think this one is - at least today - irrelevant.

    • A timeout error occurs, which means, reasonably enough, that

      timeout is not 0 and since the request started the amount of milliseconds specified by timeout has passed

As far as XMLHttpRequest goes, that's everything.

Beyond XMLHttpRequest, I would speculate that HTTP libraries in languages outside of JavaScript may well be using a 0 status code similarly, as a default value when no status code has been received from the server.

这篇关于是否为0的HTTP状态code有什么意义呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆