gpt4 book ai didi

javascript - ExtJS 4 - 在 REST 代理和/或存储中访问自定义响应 header ?

转载 作者:可可西里 更新时间:2023-11-01 16:28:45 26 4
gpt4 key购买 nike

我从这个问题中学习了如何将自定义 header 添加到 Ext-JS 请求:

HTTP Headers with ExtJS 4 Stores

现在我正在尝试处理当问题是用户错误时的异常处理问题。这个问题在这里处理:

Understanding REST: Verbs, error codes, and authentication

其中一个答案建议映射到最接近的 HTTP 错误代码,然后还使用自定义响应 header 将异常信息传回客户端。

I'd strongly advise against extending the basic HTTP status codes. If you can't find one that matches your situation exactly, pick the closest one and put the error details in the response body. Also, remember that HTTP headers are extensible; your application can define all the custom headers that you like. One application that I worked on, for example, could return a 404 Not Found under multiple circumstances. Rather than making the client parse the response body for the reason, we just added a new header, X-Status-Extended, which contained our proprietary status code extensions. So you might see a response like:

HTTP/1.1 404 Not Found<br/>
X-Status-Extended: 404.3 More Specific Error Here

我非常喜欢这个想法。但无法弄清楚如何从 ExtJS 中访问此自定义响应 header 以提取异常消息。

最佳答案

Response 对象有一个 getAllResponseHeader() 方法,您可以使用该方法访问所有 header 键值对。

在 Extjs 中你可以这样做:

Ext.Ajax.request({
url: 'www.microsoft.com',
method: 'GET',
failure: function(response){
console.log(response.getAllResponseHeaders());
}
});​

这将返回一个包含以下信息的实例:

connection: "keep-alive"content-encoding: "gzip"content-type: "text/html; charset=utf-8"date: "Thu, 13 Dec 2012 03:10:15 GMT"server: "nginx/0.8.54"transfer-encoding: "chunked"vary: "Cookie"

In your case you should do the following:

    failure: function(response){
var header = response.getAllResponseHeaders(),
statusEx = header['X-Status-Extended'];

if(statusEx === '404.3 More Specific Error Here'){
// do something here
}
}

关于javascript - ExtJS 4 - 在 REST 代理和/或存储中访问自定义响应 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13847374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com