Can not call Mvc Controller Action from AngularJs Controller with parameters

27,324

Solution 1

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"}
});

Solution 2

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);
    });
}]);
Share:
27,324
Ajay Kumar
Author by

Ajay Kumar

Updated on August 02, 2022

Comments

  • Ajay Kumar
    Ajay Kumar almost 2 years

    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.