2013-10-21 111 views
2

我使用php urlencoding url字符串,然後通過curl將它傳遞給phantomjs腳本,我試圖用javascript解碼它。在javascript中解碼url的問題

我開始有:

localhost:7788/hi there/how are you/ 

它獲取變成:通過用urlencode()函數

localhost:7788/hi+there%2Fhow+are+you 

在PHP端。

在phantomjs方面,我有:

// Create serever and listen port 
server.listen(port, function(request, response) {  

function urldecode(str) { 
    return decodeURIComponent((str+'').replace(/\+/g, '%20')); 
}  


     // Print some information Just for debbug 
     console.log("We got some requset !!!"); 
     console.log("request method: ", request.method); // request.method POST or GET 
     console.log("Get params: ", request.url); //  

      url= urldecode(request.url); 

     //-- Split requested params 
     var requestparams = request.url.split('/');  


     console.log(urldecode(requestparams[1])); 
     console.log(urldecode(requestparams[2])); 

在控制檯輸出:

..... 
request method: GET 
Get params: /hi%2Bthere/how%2Bare%2Byou 
hi+there 
how+are+you 

爲什麼的 '+' 號不帶空格取代?我試圖擺脫他們,它在我看來,函數'urldecode'應該這樣做。

+0

爲什麼首先有'+' - 標誌? –

回答

4

您應該使用rawurlencode()代替urlencode()在PHP一邊,所以空間與%20編碼,而不是+跡象,因此JavaScript可以得很好用decodeURIComponent()解碼。

+0

空格是'%20' – Izkata

+0

謝謝,它的工作現在。 – user61629