我是AngularJS的新手。我想从我的角度控制器向MVC5控制器进行ajax调用。我带有三个参数的动作驻留在HomeController中,例如
public ActionResult addFrnd(int name, string phone, int age)
{
//do something
return json();
}
我的角度控制器去了
var myModule = angular.module("myApp", ['ngRoute']);
myModule.controller('SimpleController', function ($scope, $http, SimpleFactory) {
$scope.friends = [];
init();
function init() {
$scope.friends = SimpleFactory.getFriend();
}
$scope.addFriend = function ($http) {
$scope.friends.push(
{
name: $scope.newFriend.name,
phone: $scope.newFriend.phone,
age: $scope.newFriend.age
});
//$http("/Home/addFrnd");
$http.get({
type: "POST",
url: "~/Home/addFrnd",
data: {
name: $scope.newFriend.name,
phone: $scope.newFriend.phone,
age: $scope.newFriend.age
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
},
error: function (msg) {
alert(msg.d);
}
});
};
});
我怎样才能解决这个问题?请帮我
其中一些可能取决于您的路由。无论哪种方式,您都需要在http通话中删除“〜”后再返回首页。另一个问题是您正在使用$ http.get,而您指定的类型是帖子。如果实际上是帖子,则只需使用$ http.post({。另外,请在您的mvc控制器中,使用post属性指定此调用为帖子:
[HttpPost]
public ActionResult addFrnd(int name, string phone, int age)
{
//do something
return json();
}
您可以像这样按角度编写帖子:
$scope.addFriend = function(){
addFriend()
.success(function(returnVal){
//success
})
.err(function(error){
//error
});
};
function addFriend(){
var data = {
name: $scope.newFriend.name,
phone: $scope.newFriend.phone,
age: $scope.newFriend.age
};
return $http.post('/Home/addFrnd/', data);
}
同样也不需要将$ http传递到$ scope.addFriend的作用域函数中,您已经注入了它
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句