如何为服务器构建角度$ resource POST请求?

Dar*_*kes 4 javascript asp.net asp.net-mvc angularjs angular-resource

我无法从AngularJS的服务器端控制器上捕获ASP.NET MVC项目的请求:

    var appDirective = angular.module('app.directive', []);
    var xmplService = angular.module('app.service', ['ngResource']);
    var appFilter = angular.module('app.filter', []);        
    var app = angular.module('app', ['app.service', 'app.directive', 'app.filter', 'solo.table']);
    xmplService
    .factory('testResource1', function ($resource) {
         return $resource('/SoloAngularTable/TestMethod3', { id: '@id' }, { charge: { method: 'POST' } });
    });
    app
    .controller('ContentPlaceHolder1Controller', function ($scope, $http, testResource1) {                            
         testResource1.find({ id: 123 }, function (data) {                            
              alert('success');
         });
    });
Run Code Online (Sandbox Code Playgroud)

在MVC控制器上,我有一个无法捕获请求的方法:

[HttpPost]
public JsonResult TestMethod3(int id)
{
    var data = new { User = "Alex" };
    return Json(data);
}
Run Code Online (Sandbox Code Playgroud)

如何使用AngularJS $资源和catch请求构建对服务器端的简单请求?

The*_*One 10

find不是默认操作$resource.默认值为:

{ 'get':    {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} };
Run Code Online (Sandbox Code Playgroud)

您必须创建/指定find操作.您当前正在创建/指定charge操作.也许这就是你的意思?

如果你想使用find,这里是修复:

.factory('testResource1', function ($resource) {
     return $resource('/SoloAngularTable/TestMethod3', { id: '@id' }, {
         charge: { method: 'POST' },
         find: { method: 'POST' } // Added `find` action
     });
});
Run Code Online (Sandbox Code Playgroud)

如果您打算使用charge,这是修复:

.controller('ContentPlaceHolder1Controller', function ($scope, $http, testResource1) {                            
     testResource1.charge({ id: 123 }, function (data) {                            
          alert('success');
     });
});
Run Code Online (Sandbox Code Playgroud)