2012-06-27 12 views
16

तो मैं jQuery प्राप्त करने के माध्यम से हेडर प्रतिक्रिया से स्थान प्राप्त करने का प्रयास कर रहा हूं। मैंने getResponseHeader ('location') का उपयोग करने की कोशिश की और getAllResponseHeaders() प्राप्त करें लेकिन वे दोनों शून्य वापस लौटते प्रतीत होते हैं।jQuery से प्रतिक्रिया शीर्षलेख स्थान कैसे प्राप्त करें?

यहाँ मेरे वर्तमान कोड है जब अतुल्यकालिक अनुरोध रिटर्न, तो आप success callback में उन्हें पढ़ने के लिए की आवश्यकता होगी

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

प्रतिक्रिया यूआरएल की पकड़ पाने के लिए कोई रास्ता नहीं है शायद http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call –

उत्तर

25

हेडर उपलब्ध हो जाएगा: में कुछ हेडर के लिए

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

का डुप्लिकेट किसी भी भाग्य के साथ प्रयास नहीं किया। यूआरएल एक सफल बयान वापस नहीं कर रहा है। मैंने इसे झूठ पर प्रिंट करने की कोशिश की और साथ ही कोई भाग्य – raeq

+3

'पूर्ण: फ़ंक्शन (xhr) {console.log (xhr.getAllResponseHeaders()) आज़माएं; } ' – Bergi

+3

मुझे अभी भी एक शून्य या खाली स्ट्रिंग मिलती है http://jsfiddle.net/nNwGL/1/ – raeq

3

jQuery अजाक्स आपको 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); 
    } 
}); 
तक पहुंचने की आवश्यकता है

या सादे जावास्क्रिप्ट

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 ऑब्जेक्ट सार। यह जहां वे "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. 

के बारे में बात उनके डॉक्स में है आप देख सकते हैं वहाँ क्योंकि jqXHR एपीआई यह खुलासा नहीं करता

संबंधित मुद्दे