2012-06-27 26 views
16

Vì vậy, tôi đang cố gắng để có được vị trí từ một phản ứng tiêu đề thông qua jQuery nhận được. Tôi đã thử sử dụng getResponseHeader ('Location') và getAllResponseHeaders() nhưng cả hai đều dường như trả về null.Cách nhận vị trí Tiêu đề phản hồi từ jQuery Nhận?

Đây là mã hiện tại của tôi

$(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

có lẽ là bản sao của http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call –

Trả lời

25

Các tiêu đề sẽ có sẵn khi không đồng bộ yêu cầu lợi nhuận, vì vậy bạn sẽ cần phải đọc chúng trong 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

đã cố gắng mà không có may mắn. Url không trả lại câu lệnh thành công. Tôi đã thử in nó ra trên giả cũng như không có may mắn – raeq

+3

thử một 'hoàn thành: chức năng (xhr) {console.log (xhr.getAllResponseHeaders()); } ' – Bergi

+3

Tôi vẫn nhận được chuỗi rỗng hoặc rỗng http://jsfiddle.net/nNwGL/1/ – raeq

3

đối với một số tiêu đề trong jQuery Ajax bạn cần truy cập đối tượng 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); 
    } 
}); 

hoặc sử dụng đồng bằng 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 trừu tượng hóa các đối tượng XMLHttpRequest trong một cái gọi là "siêu bộ" mà không tiếp xúc với lĩnh vực responseURL. Đó là trong tài liệu của họ, nơi họ nói về "jQuery XMLHttpRequest (jqXHR) đối tượng"

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. 

Như bạn có thể thấy không có cách nào để có được giữ của URL phản ứng vì API jqXHR không vạch trần nó

Các vấn đề liên quan