Accessing custom reponse headers within REST proxy and/or store?

I've learned how to add custom headers to Ext-JS requests from this question:

HTTP Headers with ExtJS 4 Stores

And now I am trying to deal with the issue of exception handling when the problem is a user error. This question is dealt with here:

Understanding REST: Verbs, error codes, and authentication

One of the answers suggests mapping to the closest HTTP error code but then also using a custom response header to pass exception information back to the client.

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
X-Status-Extended: 404.3 More Specific Error Here

I like this idea a lot. But cannot figure out how, from within ExtJS I could gain access to this custom response header to extract the exception message.


Response object has a getAllResponseHeader() method that you can use to have access to all the header key-value pairs.

In Extjs you can do something like this:

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

This returns an instance with this info:

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
        }
     }
链接地址: http://www.djcxy.com/p/45362.html

上一篇: 添加参数以休息GET请求

下一篇: 在REST代理和/或存储中访问自定义响应头?