2013-06-12 30 views
2

我試圖在Ext.ajax.request上讀取即將到來的響應標頭。ExtJS 4.2.1:無法在Ext.ajax.request回調時檢索HTTP響應標頭

這是代碼:

Ext.Ajax.request({ url: 'http://localhost:3000/v0.1/login' , 
    method: 'POST', 
    scope:this, 
    jsonData: {"_username":username,"_userpwd":password}, 
    success: function(responseObject){ 
     var headers = responseObject.getAllResponseHeaders(); 
     console.info(headers); 
     Ext.destroy(Ext.ComponentQuery.query('#loginWindow')); 
     this.application.getController('SiteViewController').showView(); 
    }, 
    failure: function(responseObject){ 
     alert(responseObject.status); 
    } 
    }); 

但是,它是在控制檯打印出來的只有頭:

Object {content-type: "application/json; charset=utf-8"} 

所有其他的頭不見了,但它們存在於鉻檢查員!

我錯過了什麼?由於

回答

0

我遇到同樣的問題,最後我在這裏找到了解決辦法:http://www.html5rocks.com/en/tutorials/cors/

這裏是關於標題的一部分:

Access-Control-Expose-Headers (optional) - 
The XMLHttpRequest 2 object has a getResponseHeader() method that returns the value of 
a particular response header. During a CORS request, the getResponseHeader() method 
can only access simple response headers. 
Simple response headers are defined as follows: 

Cache-Control 
Content-Language 
Content-Type 
Expires 
Last-Modified 
Pragma 

If you want clients to be able to access other headers, you have to use the 
Access-Control-Expose-Headers header. The value of this header is a comma-delimited 
list of response headers you want to expose to the client. 

我還沒有核實過,但它似乎對正確的軌道:)

1

因爲你可能正在做一個跨域請求,你將只有服務器明確公開的頭。相同的域請求公開所有標題。

在服務器端,您必須添加標題「Access-Control-Expose-Headers」,並在頭部列表中列出您想要公開的詳盡列表,並用逗號分隔。在PHP中它是這樣的:

header("Access-Control-Expose-Headers: Content-length, X-My-Own-Header"); 

的標題確實將可通過responseObject.getAllResponseHeaders()或類似responseObject.getResponseHeader('content-type')http://www.html5rocks.com/en/tutorials/cors/

PS:關於跨域請求和頭

更多信息Ace.Yin有正確的答案,但我沒有足夠的聲譽簡單地評論。