Reputation: 1842
I am trying to send parameters in GET method in angularJs.
$scope.callMethod = function(url , paramName1, paramName2 ,paramValue1 , paramValue2)
{
$http.get('rest/'+url+'?cd='+ (new Date()).getTime(),{params:{paramName1:paramValue1 ,paramName2:paramValue2}}).success(function(data)
{
console.log("in success block");
}).error(function(data)
{
console.log("in error block");
}
);
}
Here I provide the value of all the variables including the paramNames because I want this method to be reusable but during debug in browser I see that paramValues are being appended in correct way but paramNames are hardcoded. URL is as follows:
http://localhost:7001/MyWeb/rest/getProj?cd=1419222398177¶mName1=666560¶mName2=1
I have provided correct URL mapping in my spring controller. If gives me the error
406 (Not Acceptable)
Kindly tell me that is it not possible to provide keys as variables in URL. thanks in advance
Upvotes: 1
Views: 84
Reputation: 1636
You can use variables for the keys in objects, you'll just have to stringify them before hand. You can do something like New Dev suggested, which will call .toString on the variable in brackets. Or you can explicitly call JSON.stringify:
var params = {};
params[JSON.stringify(paramName1)] = paramValue1;
params[JSON.stringify(paramName2)] = paramValue2;
Using JSON.stringify is more robust, and won't return something like [object Object]
Upvotes: 0
Reputation: 49590
This is because $http
is using the keys of the params
object as the query string keys, so when you define an object literal inline, like you did, the string paramName1
became the actual key.
If you want to use the value of paramName1
to be the key, prepare your params
object like so:
var params = {};
params[paramName1] = paramValue1;
params[paramName2] = paramValue2;
and then call $http
:
$http.get('rest/' + url + '?cd=' + (new Date()).getTime(), {
params: params
})
Upvotes: 1