2012-06-27 12 views
16

Bu yüzden jQuery get yoluyla bir üstbilgi yanıtından konum almaya çalışıyorum. GetResponseHeader ('Location') ve getAllResponseHeaders() kullanmayı denedim ancak ikisi de boşa döndüklerini gösteriyor. Bazı başlıklarda içinResponse Header konumu jQuery'den nasıl alınır?

$.ajax({ 
    type: "GET", 
    url: 'http://searchlight.cluen.com/E5/Login.aspx?URLKey=uzr7ncj8)', 
    success: function(data, status, xhr) { 
     console.log(xhr.getResponseHeader('Location')); 
    } 
}); 
+0

maruz olmadığından yanıt URL tutmak için hiçbir yolu yoktur görebileceğiniz gibi Muhtemelen http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call –

cevap

25

başlıkları satışa sunulacak benim geçerli kod var jQuery Ajax XMLHttpRequest nesnesine erişmeniz gerekiyor

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); 
    } 
}); 

veya düz 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

'un çoğaltılması şanssızlık denedi. Url bir başarı beyanı vermiyor. – raeq

+3

şanssız bir şekilde false de yazdırmayı denedim: "complete: function (xhr) {console.log" (xhr.getAllResponseHeaders()); } ' – Bergi

+3

Hâlâ boş ya da boş bir dize alıyorum http://jsfiddle.net/nNwGL/1/ – raeq

3

:

İşte asenkron istek döner, sen success callback bunları okumak gerekir böylece zaman

$(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); 
}); 
-1

jQuery kullanarak responseURL alanını maruz olmayan bir sözde "süper sette" de XMLHttpRequest nesnesini soyutlar. Onlar "jQuery XMLHttpRequest (jqXHR) nesne"

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. 

hakkında konuşmak nereye onların docs öyle sen jqXHR API

İlgili konular