javascript 响应未解析,状态代码为 4xx
我在控制器中执行以下操作:
def publish
respond_to |format|
format.js do
if allowed_to_publish then
render :js => 'alert("successful");'
else
render :js => 'alert("error");'
end
end
end
end
客户端通过 ajax 请求访问方法。到目前为止,效果很好,唯一让我烦恼的是,响应的状态代码始终是“200”。
当出现错误并且无法满足请求时,根据错误类型,它应该是 4xx 或 5xx 响应。就我而言,当用户未输入所有必需的详细信息时,publish
可能会出错。我认为正确的状态代码是 412 先决条件失败。
现在,当我更改状态代码时,它不再执行 javascript:
def publish
respond_to |format|
format.js do
if allowed_to_publish then
render :js => 'alert("successful");'
else
render :js => 'alert("error");', :status => 412
end
end
end
end
客户端获得相同的响应但状态代码,并且不再执行 javascript。响应标头如下:
Date Sun, 04 Sep 2011 12:29:58 GMT
Server Apache/2.2.14 (Ubuntu)
X-Powered-By Phusion Passenger (mod_rails/mod_rack) 3.0.8
X-UA-Compatible IE=Edge
Cache-Control no-cache
X-Runtime 0.176543
Status 412
Strict-Transport-Security max-age=172800, includeSubDomains
Keep-Alive timeout=15, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/javascript; charset=utf-8
正文仍然是 alert("error");
。但它不发出警报。
这是浏览器限制吗(我使用的是 Firefox 7b),或者可以使其与 Rails 一起使用吗?
如果我想执行 javascript,我必须始终使用 200
吗?
I'm doing the following in my controller:
def publish
respond_to |format|
format.js do
if allowed_to_publish then
render :js => 'alert("successful");'
else
render :js => 'alert("error");'
end
end
end
end
Clients access the methods with an ajax-request. This works good so far, the only thing that bugs me is, that the status code of the response is always "200".
When there's an error and the request cannot be fulfilled, it should be a 4xx or 5xx response depending on the type of error. In my case, publish
can go wrong when a user does not enter all required details. The correct status code for this, I think, is 412 precondition failed.
Now, when I change the status code, it does not execute the javascript anymore:
def publish
respond_to |format|
format.js do
if allowed_to_publish then
render :js => 'alert("successful");'
else
render :js => 'alert("error");', :status => 412
end
end
end
end
The client gets the same response but the status code, and does not execute the javascript anymore. Response headers are the following:
Date Sun, 04 Sep 2011 12:29:58 GMT
Server Apache/2.2.14 (Ubuntu)
X-Powered-By Phusion Passenger (mod_rails/mod_rack) 3.0.8
X-UA-Compatible IE=Edge
Cache-Control no-cache
X-Runtime 0.176543
Status 412
Strict-Transport-Security max-age=172800, includeSubDomains
Keep-Alive timeout=15, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/javascript; charset=utf-8
and body is still the alert("error");
. But it does not alert.
Is this a browser restriction (I'm using Firefox 7b), or can this be made to work with rails?
Must I always use 200
if I want to have my javascript to be executed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个状态码都有其含义,200 表示请求已成功完成。当您收到其他 4xx 或 5xx 代码时,这意味着那里存在一些错误,并且脚本尚未运行/返回成功的响应。在这种情况下,您不应期望得到相同的输出,但是您可以在 Javascript 代码中处理这种情况,并显示一些有意义的消息,以防请求出现错误。
Every status code has a meaning and 200 means The request was fulfilled successfully. When you are getting other 4xx or 5xx codes that means that there is some error there and the script has not run/returned a successful response. In that case you should not expect the same output, however you can handle this case in your Javascript code and display some meaningful message incase something wrong occured with the request.
如果除
200
以外的任何内容造成问题,为什么不直接返回它呢。无论如何,除非深入研究 HTTP 标头,否则没有人会真正看到响应代码。我不知道为什么它不起作用,但老实说我认为你不应该让事情变得太困难。
毕竟,HTTP 请求成功了,所以
200
的 HTTP 响应确实是非常合适的。If anything other than
200
causes trouble, why not just return that. Nobody will actually see the response code unless they are digging into the HTTP headers, anyway.I don't know why it isn't working, but I honestly think you shouldn't make things too difficult for yourself.
After all, the HTTP request was successful, so an HTTP response of
200
is very appropriate indeed.