我遇到了一种情况,我需要使用 GET 方法在 AngularJS 中使用 Restful Web 服务,方法是允许它接受 uri 参数中的斜杠“/”字符。 通常“/”斜杠会创建不同的端点,并且服务不会给出所需的响应,我需要使用 RESTful Web 服务,其中参数应作为字符串传递。
要考虑的场景:
示例网址: http://hostname/servicename/ {范围} 其中参数应该是字符串并且应该对以下示例输入有效
- 一个
- 12
- 12/15
- 126/567
我正在使用下面的代码
service.js
angular.module('starter.services', [])
.factory('dataService', ['$http', function($http) {
var obj = {};
obj.getData = function(url){
return $http({
method: 'GET',
url: url,
headers: {'Content-Type': 'application/json;charset=utf-8'},
}).then(function successCallback(response) {
return response.data;
}, function errorCallback(response) {
return "ERROR";
});
}
return obj;
}])
controller.js
var url = "http://hostname/servicename/" + paramId + "";
dataService.getData(url).then(
function(response) {
// Response stuff here
}
)
注意:我必须在客户端管理所有事情,并且无权访问 Web 服务的服务器端代码。
最佳答案
像这样对参数进行编码。
encodeURIComponent(paramId)
否则将/替换为“%2f”
关于javascript - 在 Angular JS 的 uri 参数中使用 'slash' 字符使用 Restful 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34986781/