如何使用angular js $ http post调用控制器[HttpPost]方法?

时间:2015-04-06 17:03:56

标签: angularjs angularjs-http

我有一个控制器方法如下。

public class ProcessorController : System.Web.Http.ApiController
{

    [HttpPost]
    public void Paid(string confirmationNumber)
   {

   }
}

我试图从角度js中的函数调用此方法,如下所示。 $ http.post不起作用。当它试图点击$ http.post中指定的路径时,我在fiddler中看到错误'找不到资源'。谁能指出这里出了什么问题?谢谢!

var payControllers = angular.module('payControllers', []);
payControllers.controller('payCtrl', ['$scope', '$http',
       function ($scope, $http) {

        $scope.Process = function(confnumber) {
        $scope.ConfNumber = confnumber;

            if ($scope.ConfNumber.length > 0) {
                    $http.post('/Processor/Paid',
                       {
                           confirmationNumber: $scope.ConfNumber
                       }
                     ).success(function () {
                         alert('updated')
                     });
              }
    }
}]);

1 个答案:

答案 0 :(得分:2)

试一试:

var payControllers = angular.module('payControllers', []);
payControllers.controller('payCtrl', ['$scope', '$http',
       function ($scope, $http) {

        $scope.Process = function(confnumber) {
        $scope.ConfNumber = confnumber;

            if ($scope.ConfNumber.length > 0) {
                    $http({
                           url: '/api/Processor/Paid?confirmationNumber='+$scope.ConfNumber,
                           method: 'POST'
                    }).success(function (data) {
                        alert('updated');
                    });
              }
    }
}]);