40

Assume that I have two input boxes with corresponding ng-model as fname and lname. If I call http request as :

$http({method:'GET', url:'/search', params:{fname: fname, lname: lname}})

will it call to the url :

/search?fname=fname&lname=lname

The error I am getting at the backend(python) is :

cannot concatenate str and nontype objects.

Are these parameters not sent as strings? If not, how to get around with this?

4 Answers 4

74

Here is how you do it:

$http.get("/url/to/resource/", {params:{"param1": val1, "param2": val2}})
    .then(function (response) { /* */ })...

Angular takes care of encoding the parameters.

Maxim Shoustin's answer does not work ({method:'GET', url:'/search', jsonData} is not a valid JavaScript literal) and JeyTheva's answer, although simple, is dangerous as it allows XSS (unsafe values are not escaped when you concatenate them).

4
  • do we put values in .then
    – Winnemucca
    Aug 28, 2015 at 19:41
  • @stevek, you pass a function to the then clause; that function accepts a single parameter, i.e. the response itself.
    – sleblanc
    Jan 14, 2016 at 20:02
  • For me it requires 'parameter' field wrapping data like: $http.get( "/url/to/resource/", { params: { parameters: {"param1": val1, "param2": val2} } } ).then(function (response) { /* */ })
    – Shiv
    Jul 7, 2016 at 12:38
  • And for PUT and POST you need to use another method signature stackoverflow.com/questions/31572937/… Oct 26, 2016 at 10:39
16

Build URL '/search' as string. Like

"/search?fname="+fname"+"&lname="+lname

Actually I didn't use

 `$http({method:'GET', url:'/search', params:{fname: fname, lname: lname}})` 

but I'm sure "params" should be JSON.stringify like for POST

var jsonData = JSON.stringify(
    {
        fname: fname,
        lname: lname 
    }
);

After:

$http({
  method:'GET',
  url:'/search',
  params: jsonData
});
0
2

Here is a simple mathed to pass values from a route provider

//Route Provider
$routeProvider.when("/page/:val1/:val2/:val3",{controller:pageCTRL, templateUrl: 'pages.html'});


//Controller
$http.get( 'page.php?val1='+$routeParams.val1 +'&val2='+$routeParams.val2 +'&val3='+$routeParams.val3 , { cache: true})
        .then(function(res){
            //....
        })
0

We can use input data to pass it as a parameter in the HTML file w use ng-model to bind the value of input field.

<input type="text" placeholder="Enter your Email" ng-model="email" required>

<input type="text" placeholder="Enter your password " ng-model="password" required> 

and in the js file w use $scope to access this data:

$scope.email="";
$scope.password="";

Controller function will be something like that:

 var app = angular.module('myApp', []);

    app.controller('assignController', function($scope, $http) {
      $scope.email="";
      $scope.password="";

      $http({
        method: "POST",
        url: "http://localhost:3000/users/sign_in",
        params: {email: $scope.email, password: $scope.password}

      }).then(function mySuccess(response) {
          // a string, or an object, carrying the response from the server.
          $scope.myRes = response.data;
          $scope.statuscode = response.status;

        }, function myError(response) {
          $scope.myRes = response.statusText;
      });
    });

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.