0

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?

3

4 Answers 4

1

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;
     });
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0
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; });`

Comments

0

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.