2017-01-20 58 views
0

我正在嘗試使用anuglarjs http GET請求從Rest API控制器獲取數據。但是,我需要發送的參數包括「。」導致網址失敗的符號。我試圖編碼它,但它不起作用。下面是我做的:如何在angularJS中對URL進行編碼

function getApplication($http) { 
    this.getData = function(application,name) { 
     return $http({ 
     method : 'GET', 
     url : http://localhost:8080/app/rest/+ 'app/' + encodeURIComponent(name) 
     }).then(function successCallback(response) { 
     return response.data; 
     }, function errorCallback(response) { 
     console.log(response.statusText); 
     }); 
    } 
    } 

名稱參數是app.01.com

和URL結果我得到的是:

GET http://localhost:8080/app/rest/app/app.01.com 406 (Not Acceptable) 

任何人都知道如何編碼的URL,這樣我可以得到的數據從休息控制器? 謝謝

+0

通行證名稱作爲請求主體的對象。方法:'GET', url:http:// localhost:8080/app/rest/+'app /',data:name } – jbrown

+0

聽起來就像服務端的一個不好的實現。您可以嘗試用'%2E'手動替換'.'。如果這有效,那麼服務在解析規則上過於嚴格。 –

回答

1

使用BTOA和ATOB

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<p>Click the button to decode a base-64 encoded string.</p> 
 

 
<button onclick="myFunction()">Try it</button> 
 

 
<p><strong>Note:</strong> The atob() method is not supported in IE9 and earlier.</p> 
 

 
<p id="demo"></p> 
 

 
<script> 
 
function myFunction() { 
 
    var str = "Hello World!"; 
 
    var enc = window.btoa(str); 
 
    var dec = window.atob(enc); 
 

 
    var res = "Encoded String: " + enc + "<br>" + "Decoded String: " + dec; 
 
    document.getElementById("demo").innerHTML = res; 
 
} 
 
</script> 
 

 
</body> 
 
</html>

function getApplication($http) { 
    this.getData = function(application,name) { 
     return $http({ 
     method : 'GET', 
     url : http://localhost:8080/app/rest/+ 'app/' + window.btoa(name) 
     }).then(function successCallback(response) { 
     return response.data; 
     }, function errorCallback(response) { 
     console.log(response.statusText); 
     }); 
    } 
    } 
+0

非常感謝!它工作得很好! – RLe

0

這不可能是「。」是一個毫無保留的字符,並且「用相應的百分比編碼的US-ASCII八位字節替換未預留字符的URI是相同的」。因此,/%2E與/相同。 ,這將被正常化。

那麼你可以使用localhost:8080/app/rest/app/app%2E01.com爲你的URL

相關問題