Teddu
Teddu

Reputation: 209

How to append dynamic query parameter in HTTP get URL in Angular JS?

I am new to angular JS. I have one controller (.js file) in which I wrote a function to make a http get call to back-end. Like below:

             $http({
                 url: "services/rest/1.0/project/employeeDetails",
                 method: 'GET',
                 headers: config,
                 transformResponse: function (data) {
                     var x2js = new X2JS();
                     var json = x2js.xml_str2json(data);
                     return json;
                 }
             }).success(function (response) {
                 alert("success for account details with response:"+response);
                if (response && response.siteDetailsList.errorCode == 0)
                     $scope.accountdetails = response;
             });

Now problem is that I need to add two query parameter to my url which I mentioned in above code snippet , so that final URL will look like this:

services/rest/1.0/project/employeeDetails ? param1=world & param2=hello

This param1 and param2 value I am getting from my HTML file's input text box. Any idea how we append dynamic query parameter to this URL?

Upvotes: 0

Views: 3457

Answers (4)

Teddu
Teddu

Reputation: 209

I solved the problem. Its very simple. We can modify $url like this way too:

$http({
         url: "services/rest/1.0/project/employeeDetails?param1="+value+"&param2="+value",
         method: 'GET',
         headers: config
         }

By using a separate "param" property is a bit tricky because parameter get sorted alphabetically by default , so we need to write code for preventing sorting if we want parameter to appear in our desired order.

Any lastly, Thanks for your prompt responses. :)

Upvotes: 0

user3414092
user3414092

Reputation: 77

var obj = 
{
  param1:"world",
  param2:"hello"
}

$http.post("services/rest/1.0/project/employeeDetails", obj)
.then(function (response)
{
        return response`enter code here`;
},`function (error)

{ return error; });`

Upvotes: 0

Fritz
Fritz

Reputation: 171

You can use the $httpParamSerializer service from AngularJS.

https://docs.angularjs.org/api/ng/service/$httpParamSerializer

Object:

var obj = {
  param1:"world",
  param2:"hello"
}

With the httpParamSerializer:

$httpParamSerializer(obj)

Returns:

param1=test&param2=world

Upvotes: 1

Bartek Fryzowicz
Bartek Fryzowicz

Reputation: 6684

You can use params config property:

   $http({
         url: "services/rest/1.0/project/employeeDetails",
         method: 'GET',
         headers: config,
         params: {
             param1: someValue,
             param2: anotherValue
         },
         transformResponse: function (data) {
             var x2js = new X2JS();
             var json = x2js.xml_str2json(data);
             return json;
         }
     }).success(function (response) {
         alert("success for account details with response:"+response);
        if (response && response.siteDetailsList.errorCode == 0)
             $scope.accountdetails = response;
     });

Upvotes: 1

Related Questions