如何在 angularJS 中编码 URL
How to encode URL in angularJS
我正在尝试使用 anuglarjs http GET 请求从我的 Rest API 控制器获取数据。但是,我需要发送的参数包括“。”导致 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 以便我可以从 Rest Controller 获取数据吗?
谢谢
您是否尝试将 url 数据放入双引号中?
url : "http://localhost:8080/app/rest/+ 'app/' + encodeURIComponent(名称)"
“.”是不可能的。是一个非保留字符,"URIs that differ in the replacement of an unreserved character with its corresponding percent-encoded US-ASCII octet are equivalent"。因此,/%2E 与 / 相同。 ,这将被标准化。
然后您可以使用 localhost:8080/app/rest/app/app%2E01.com
作为您的网址
使用 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);
});
}
}
我正在尝试使用 anuglarjs http GET 请求从我的 Rest API 控制器获取数据。但是,我需要发送的参数包括“。”导致 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 以便我可以从 Rest Controller 获取数据吗? 谢谢
您是否尝试将 url 数据放入双引号中? url : "http://localhost:8080/app/rest/+ 'app/' + encodeURIComponent(名称)"
“.”是不可能的。是一个非保留字符,"URIs that differ in the replacement of an unreserved character with its corresponding percent-encoded US-ASCII octet are equivalent"。因此,/%2E 与 / 相同。 ,这将被标准化。
然后您可以使用 localhost:8080/app/rest/app/app%2E01.com
作为您的网址
使用 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);
});
}
}