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

标签 javascript web-services http rest extjs

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

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/

相关文章:

javascript - 在 AngularJS 中,当 localStorage 已满时,如何捕获 ngStorage 的错误?

javascript - 对象未更新 Angular

javascript - JQuery 的 Web 服务调用失败

iphone - 我们如何在 iPhone 中处理动态 Web 服务?

web-services - 为其他人实现和我的软件使用定义网络服务的最佳方式

c# - HTTPS Web 请求失败

C#-HttpWebRequest-POST

javascript - 生成按钮

javascript - 从标签获取值而不使用 id 或 class

java - 如何在 Maven 的 settings.xml 中为 HTTP 和 HTTPS 配置代理服务器?