没有提供用于所需参数的HTTP状态代码? [英] Which HTTP status code to use for required parameters not provided?

查看:373
本文介绍了没有提供用于所需参数的HTTP状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个设计用AJAX调用的页面 - 如果它们无法显示,我会让它们返回异常状态代码,并且我的javascript会相应地显示错误框。

I have several pages designed to be called with AJAX - I have them return an abnormal status code if they can't be displayed, and my javascript will show an error box accordingly.

例如,如果用户未经过身份验证或其会话超时且他们尝试调用其中一个AJAX页面,则会返回 401 Unathorized

For example, if the user is not authenticated or their session has timed out and they try to call one of the AJAX pages, it will return 401 Unathorized.

我还有一些返回 500内部服务器错误如果在服务器端发生了奇怪的事情。

I also have some return 500 Internal Server Error if something really odd happens server-side.

如果在没有必要参数的情况下调用其中一个页面,我应该返回什么状态代码? (因此不能返回任何内容)。

What status code should I return if one of these pages was called without required parameters? (and therefore can't return any content).

我看了维基百科有关HTTP状态代码的文章,但我能找到的最接近我正在寻找的代码是:

I had a look at the wikipedia article on HTTP status codes, but the closest one I could find to the code I'm looking for was this:


422无法处理的实体

请求格式正确但由于语义错误而无法遵循。

422 Unprocessable Entity
The request was well-formed but was unable to be followed due to semantic errors.

编辑:以上代码是特定于WebDAV的,因此在这种情况下不太合适

有没有人能想到要返回的相应代码?

Can anyone think of an appropriate code to return?

推荐答案


如果有的话我应该返回什么状态代码没有必要的参数调用这些页面? (因此不能返回任何内容)。

What status code should I return if one of these pages was called without required parameters? (and therefore can't return any content).

你可以选择 404 Not Found


服务器未找到与Request-URI匹配的任何内容 [假设您的所需参数是URI的一部分,即 $ _ GET ] 。没有说明该病症是暂时的还是永久性的。如果服务器通过一些内部可配置的机制知道旧资源永久不可用且没有转发地址,则应该使用410(Gone)状态代码。当服务器不希望明确拒绝请求的原因,或者没有其他响应适用时,通常会使用此状态代码。

The server has not found anything matching the Request-URI [assuming your required parameters are part of the URI, i.e. $_GET]. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

(由我突出显示)

404 Not Found 400的子集错误的请求也可以采取,因为它非常清楚这是什么:

404 Not Found is a subset of 400 Bad Request which could be taken as well because it's very clear about what this is:


请求不能由于语法错误,服务器可以理解。客户端不应该在没有修改的情况下重复请求。

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

我实际上不建议你选择一个不存在的WEBDAV响应代码对于使用超文本的HTTP客户端,但您可以,它是完全有效的,您是服务器编码器,您实际上可以采用您认为适合您的HTTP客户端的任何HTTP共振状态代码:

I can't actually suggest that you pick a WEBDAV response code that does not exist for HTTP clients using hypertext, but you could, it's totally valid, you're the server coder, you can actually take any HTTP resonse status code you see fit for your HTTP client of which you are the designer as well:


11.2。 422不可处理的实体

11.2. 422 Unprocessable Entity

422(不可处理的实体)状态代码表示服务器
了解请求实体的内容类型(因此
415(不支持的媒体类型)状态代码是不合适的),并且请求实体的
语法是正确的(因此400(错误请求)
状态代码是不合适的)但是无法处理包含的
说明。例如,如果XML
请求主体包含格式正确(即语法正确),但语义错误的XML指令
,则可能会出现此错误情况。

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

IIRC请求实体是请求正文。因此,如果您正在使用请求机构,那么Julian写道可能是合适的。

IIRC request entity is the request body. So if you're operating with request bodies, it might be appropriate as Julian wrote.

您评论道:


恕我直言,400的文字说出格式错误的语法。我假设这里的语法与客户端发送到服务器的HTTP字符串的语法有关。

可能是,但它可以是任何语法表达的,整个请求,只有一些请求标头,或特定的请求标头,请求URI等.400不是具体关于HTTP字符串语法,它实际上是一个答案的一般答案客户端错误:

That could be, but it can be anything syntactically expressed, the whole request, only some request headers, or a specific request header, the request URI etc.. 400 Is not specifically about "HTTP string syntax", it's infact the general answer to a client error:


4xx类状态代码适用于客户端似乎有错误的情况。除了在响应HEAD请求时,服务器应该包括一个实体,其中包含错误情况的解释,以及它是暂时的还是永久的。这些状态代码适用于任何请求方法。用户代理应该向用户显示任何包含的实体。

The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents SHOULD display any included entity to the user.

这里的重要部分是你必须告诉客户端出了什么问题。状态代码只是告诉出现问题(在4xx类中),但HTTP并未专门设计为使缺少的query-info部分参数可作为错误条件记录。事实上,URI只知道有一个查询信息部分而不是它意味着什么。

The important part is here that you must tell the client what went wrong. The status code is just telling that something went wrong (in the 4xx class), but HTTP has not been specifically designed to make a missing query-info part parameter noteable as error condition. By fact, URI only knows that there is a query-info part and not what it means.

如果您认为400太宽泛我建议您选择404如果问题与URI相关,例如 $ _ GET 变量。

If you think 400 is too broad I suggest you pick 404 if the problem is URI related, e.g. $_GET variables.

这篇关于没有提供用于所需参数的HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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