2012-06-05 24 views
1

我想要的路徑名稱後,我請求一個URL我設置。我可以從URL給我的路徑在jQuery中給?

讓的說,我把http://www.abc.com

那麼服務器會自動返回我http://www.abc.com/sessionId/folder/default.aspx

,我需要得到jQuery的返回URL。

無論如何要做到這一點?

我試着ajax get/post來獲取響應頭的位置,它總是得到空值。

是一個參考代碼-i顯示在下面

$.ajax({ 
    type: 'POST', 
    url: '/echo/html', 
    data: {}, 
    dataType: "json", 
    success: function(res,status,XHR) { 
     //var location = XHR..getResponseHeader('Location'); 
      alert(XHR.getResponseHeader('Content-Type')); 
      alert(XHR.getResponseHeader('Location')); 
    }, 
    error: function(jqXHR) { } 
    });​ 

回答

1
var result = 'http://www.abc.com/sessionId/folder/default.aspx', 
    request = 'http://www.abc.com'; 

console.log(result.substring(request.length)); // /sessionId/folder/default.aspx 

http://jsfiddle.net/zerkms/zNN4D/

+0

先生,我不知道,但你應該得票。 +1爲您先生 – thecodeparadox

0

您可以使用此位置對象:

http://www.w3schools.com/jsref/obj_location.asp

要創建一個從網址:

var url = document.createElement('a'); 
url.href = "http://www.abc.com/sessionId/folder/default.aspx"; 
console.log(url.pathname); // this is what you need. 

的鏈接顯示了更多的選擇,從協議哈希等

0
var url = 'http://www.abc.com/sessionId/folder/default.aspx', 
    cutting = 'http://www.abc.com'; 

console.log(url.replace(cutting,'')); 

DEMO

此外,如果你希望能夠跟隨@zerkms的解決方案。

+0

好奇爲什麼這個答案得到upvote和我沒有(雖然同樣的想法,這是10分鐘後) – zerkms

相關問題