Reputation: 462
I have a situation where I need to consume a restful web service in AngularJS using GET method by allowing it to accept slash "/" character in uri parameter. Normally "/" slash, creates a different end point and service doesn't give the required response and I need to consume the RESTful web service where the parameter should be passed as string.
Scenario to be considered:
Sample URL: http://hostname/servicename/{parameter} where parameter should be a string and Should be valid for below sample inputs
I am using below code
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
}
)
NOTE: I have to manage all things at client side and don't have access to server side code of web service.
Upvotes: 0
Views: 1836
Reputation: 544
Encode the parameter like this.
encodeURIComponent(paramId)
Otherwise replace / with '%2f'
Upvotes: 3
Reputation: 332
Have u tried params object instead passing the parameter directly in the url?
If no.. just pass the parameter as shown below.
service.js
angular.module('starter.services', [])
.factory('dataService', ['$http', function($http) {
var obj = {};
obj.getData = function(url, paramId){
return $http({
method: 'GET',
url: url,
params:{
"paramId" : paramId
},
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";
dataService.getData(url, paramId).then(
function(response) {
// Response stuff here
}
);
Let me know if this helps!
Upvotes: 1