2012-06-27 12 views
16

私は、jQuery経由でヘッダレスポンスから位置を取得しようとしています。私はgetResponseHeader( 'Location')とgetAllResponseHeaders()を使ってみましたが、両方ともnullを返すようです。jQueryからレスポンスヘッダの位置を取得するには?

ここに私の現在のコード

$(document).ready(function(){ 
    var geturl; 
    geturl = $.ajax({ 
     type: "GET", 
     url: 'http://searchlight.cluen.com/E5/Login.aspx?URLKey=uzr7ncj8)', 
    }); 
    var locationResponse = geturl.getResponseHeader('Location'); 
    console.log(locationResponse); 
}); 
+0

を公開していないため、応答URLのホールドを取得する方法はありません見ることができるようにそれは、彼らが「jQueryのはXMLHttpRequest(jqXHR)オブジェクト」

For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods: readyState responseXML and/or responseText when the underlying request responded with xml and/or text, respectively status statusText abort([ statusText ]) getAllResponseHeaders() as a string getResponseHeader(name) overrideMimeType(mimeType) setRequestHeader(name, value) which departs from the standard by replacing the old value with the new one rather than concatenating the new value to the old one statusCode(callbacksByStatusCode) No onreadystatechange mechanism is provided, however, since done, fail, always, and statusCode cover all conceivable requirements. 

の話を自分のドキュメントにありますおそらく、http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call –

答えて

25

非同期要求リターンヘッダが利用できるようになりますので、あなたはsuccess callbackでそれらを読み込む必要がありますです:

でいくつかのヘッダの
$.ajax({ 
    type: "GET", 
    url: 'http://searchlight.cluen.com/E5/Login.aspx?URLKey=uzr7ncj8)', 
    success: function(data, status, xhr) { 
     console.log(xhr.getResponseHeader('Location')); 
    } 
}); 
+1

の複製は運がないと試みました。 urlはsuccess文を返していません。私は幸運でも偽でそれを印刷しようとしました – raeq

+3

'complete:function(xhr){console.log(xhr.getAllResponseHeaders()); } ' – Bergi

+3

まだ空文字列か空文字列がありますhttp://jsfiddle.net/nNwGL/1/ – raeq

3

は、 jQuery AjaxでXMLHttpRequestオブジェクトにアクセスする必要があります

var xhr; 
var _orgAjax = jQuery.ajaxSettings.xhr; 
jQuery.ajaxSettings.xhr = function() { 
    xhr = _orgAjax(); 
    return xhr; 
}; 

$.ajax({ 
    type: "GET", 
    url: 'http://example.com/redirect', 
    success: function(data) { 
     console.log(xhr.responseURL); 
    } 
}); 

またはプレーンjavascriptの

var xhr = new XMLHttpRequest(); 
xhr.open('GET', "http://example.com/redirect", true); 

xhr.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
    console.log(xhr.responseURL); 
    } 
}; 

xhr.send(); 
-1

jQueryを使用してはresponseURLフィールドを公開しない、いわゆる「スーパーセット」でXMLHttpRequestオブジェクトを抽象化します。あなたはjqXHR APIはそれ

関連する問題