Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 948
Ajay Verma Avatar asked Jan 27 '15 08:01

Ajay Verma


2 Answers

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"}
});
like image 130
Aniket Kulkarni Avatar answered Nov 19 '22 23:11

Aniket Kulkarni


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);
    });
}]);
like image 3
Omri Aharon Avatar answered Nov 20 '22 01:11

Omri Aharon