Ajay Verma
Ajay Verma

Reputation: 564

Can not call Mvc Controller Action from AngularJs Controller with parameters

I am trying to call my Mvc Controller Action from Angularjs controller $http.get(). Please provide some solution.

Controller Action :

 [HttpGet]
 public string CallMe(int number,string name)
 {
     return "Welcome " + name +" to Angular Js " + number + " times.";
 }

Angular JS Controller

 app.controller("StoreController", ["$http", function ($http) {        
    $http.get("/Home/CallMe", { number: 4, name: "angular"     }).success(function (data) {
        console.log(data);
    }).error(function (error) {           
        console.log(error);
    });
}]);

Also, Could anyone please specify related material for learning?

Thanks in advance.

Upvotes: 9

Views: 27744

Answers (2)

Aniket Kulkarni
Aniket Kulkarni

Reputation: 12983

You are using $http#get method.

get(url, [config]);

 Param    Type     Details
 url      string   Relative or absolute URL specifying the destination of the request

 config   Object   Optional configuration object
(optional)

For passing parameters angular.http provides an option for it params

$http({
   url: "/Home/CallMe", 
   method: "GET",
   params: {number: 4, name: "angular"}
});

Upvotes: 12

Omri Aharon
Omri Aharon

Reputation: 17064

You're trying to use the get method with post parameters. get only takes a URL parameter, and an optional config one. Take a look here at the documentation.

Try to append your parameters to the URL:

 app.controller("StoreController", ["$http", function ($http) {        
    $http.get("/Home/CallMe?number=4&name=angular").success(function (data) {
        console.log(data);
    }).error(function (error) {           
        console.log(error);
    });
}]);

Upvotes: 3

Related Questions