无法使用参数从AngularJs Controller调用Mvc控制器操作

时间:2022-07-19 21:06:26

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

我试图从Angularjs controller $ http.get()调用我的Mvc Controller Action。请提供一些解决方案。

Controller Action :

控制器动作:

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

Angular JS Controller

Angular JS控制器

 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.

提前致谢。

2 个解决方案

#1


13  

You are using $http#get method.

您正在使用$ http#get方法。

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

对于传递参数,angular.http为它提供了一个参数params

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

#2


3  

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.

您正在尝试将get方法与post参数一起使用。 get只接受一个URL参数和一个可选的配置参数。看看文档。

Try to append your parameters to the URL:

尝试将您的参数附加到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);
    });
}]);

#1


13  

You are using $http#get method.

您正在使用$ http#get方法。

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

对于传递参数,angular.http为它提供了一个参数params

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

#2


3  

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.

您正在尝试将get方法与post参数一起使用。 get只接受一个URL参数和一个可选的配置参数。看看文档。

Try to append your parameters to the URL:

尝试将您的参数附加到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);
    });
}]);