【问题标题】:how to build angular $resource POST request to server?如何构建对服务器的角度 $resource POST 请求?
【发布时间】:2014-02-22 18:55:22
【问题描述】:

我无法在 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');
         });
    });

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

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

如何使用 AngularJS $resource 构建对服务器端的简单请求并捕获请求?

【问题讨论】:

    标签: javascript asp.net asp.net-mvc angularjs angular-resource


    【解决方案1】:

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

    { 'get':    {method:'GET'},
      'save':   {method:'POST'},
      'query':  {method:'GET', isArray:true},
      'remove': {method:'DELETE'},
      'delete': {method:'DELETE'} };
    

    您必须创建/指定find 操作。您当前正在创建/指定 charge 操作。也许这就是您要使用的?

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      相关资源
      最近更新 更多