【问题标题】:Posting data with angularjs v 1.3使用 angularjs v 1.3 发布数据
【发布时间】:2015-01-25 15:03:36
【问题描述】:

我有一个 web api 2.0 应用程序,我必须发布表单的数据。这里是我的 js 代码:

(function (angular) {
    var public_area = angular.module("public-area", ['ngResource']);

    public_area.factory("MyService", function ($resource) {
        return {
            my_controller: $resource("/api/MyHttpControllerTest/")
        };
    });

    public_area.controller("MyController", function ($scope, MyService) {
        $scope.getPlans = function ()
        {
            var pianiTariffari =
                new MyService
                    .my_controller
                    .query({
                        customerType: $scope.customerType
                    }, isArray = true);
        }

        $scope.sendInvitation = function ( )
        {
            var invitation =
                 new MyService
                    .my_controller(
                        {
                            request: {
                                CustomerType: "test"
                            }
                        });

            invitation.$save();
        }
    });
})(angular);    

你怎么看,我有一个控制器..和两个函数,1个在GET和1个在POST......

我看到你也是服务器端控制器

public FillFormPresentaUnAmicoResponse GetPromozioni(string customerType)
    {
        ...
    }

    [HttpPost]
    public PresentaUnAmicoResponse InvitaAmico(PresentaUnAmicoRequest request)
    {
        ...
    }

PresetaUnAmicoRequest 类在哪里

public class PresentaUnAmicoRequest
{
    public string CustomerType { get; set; }
}

GET 功能正常工作!但是POST不是... 在帖子中,我收到了“请求”的有效实例,但其中的属性为 NULL...

对我来说真正奇怪的是,如果我以这种方式调用 Post 方法:

var invitation = new MyService.my_controller(undefined);
invitation.$save();

服务器端,“请求”是实例化的。我期望“请求 = null”代替..

谁能帮帮我? 谢谢

更新:

如果我理解的话,我总是需要同一个对象来坚持 REST 原则。所以,根据spike的例子,我相信我必须创建一个类似的对象:

public class Promos
{
   public string CustomerType{ get; set; }
   public List<string> PromoPerCustomerType{ get; set; }
}

public class PresentaAmico 
{
    public Promos Promozioni { get; set; }
    public string Name{ get; set; }
    public string Surname { get; set; }
    ...
}

而我的 Rest 服务应该是这样的

[HttpGet]
public PresentaAmico Get(string customerType)
{
     ...
}

[HttpPost] 
public void Save(PresentaAmico myForm)
{
     ...
}     

正确吗?

【问题讨论】:

    标签: angularjs resources http-post asp.net-web-api2


    【解决方案1】:

    恕我直言,这不是 $resouce 发挥最佳作用的方式,那是因为您并没有真正坚持 REST 原则。 $resource 的想法是获取一个对象,修改它,然后再次 $save 它,映射到 GET 和 POST 调用,其中返回和发送的对象是相同的。

    您正在获取 FillFormPresentaUnAmicoResponse 并发布另一种类型的对象,即 PresentaUnAmicoResponse。

    这当然可以开始整个讨论是否您使用 REST,因为它可以与 WebAPI 一起使用:即一个资源,您可以通过在其上映射 HTTP 动词(GET/POST 等)来修改它。您的控制器并不真正遵守这些规则,因为您通过网络发送不同类型的对象。

    所以我实际上会推荐 2 个控制器,每个类型一个:FillFormPresentaUnAmicoResponseController 和一个 PresentaUnAmicoResponseController。在这种情况下,我还会重新考虑命名 - 'response' 后缀中不需要。

    var presenta = $resource('/presentaunamicoresponse/:id', { id:'@id'});
    // The id doesn't make any sense here - which should raise the question on
    // whether these stuff is really suitable for REST or whether you are really
    // doing a remote procedure call, but OK.
    presenta.CustomerType = 'Whatever';
    presenta.$save();
    

    我认为这就是它的工作方式。不过,为了保持一致性,您必须将其粘贴在您的工厂中。但正如您在文档 (https://docs.angularjs.org/api/ngResource/service/$resource) 中看到的那样 - 您获得了资源,并操纵了资源本身的属性。然后你调用它的 $save() 方法。

    【讨论】:

    • 你能给我更多关于这个域的信息吗?你得到了促销 - 我想这是特别优惠 - 你想邀请朋友还是什么?抱歉,我的意大利语(如果是意大利语)非常糟糕。
    • 我创建了一个小提琴:jsfiddle.net/jctzqdcv/1 - 这对你有用吗?在级联下拉列表的情况下 - 返回树结构对我来说非常有效。您只需要一个 GET 调用即可完成这项工作。很抱歉我没有使用 $resource,如果你愿意,我也可以这样做。告诉我。
    • 1 - 是的,我认为你做到了,尽管为了清楚起见,你可以添加包装控制器。原始 REST 文档在这里,您可能会感兴趣。 ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm。此外,按照惯例,WebAPI 的工作量很大。因此,如果您只是调用方法 Get 和 Post,则不需要属性。作为额外的奖励,我为您创建了一个单选按钮示例:jsfiddle.net/jctzqdcv/2 - 请注意 ng-repeat 的 ng-model 中的 $parent。原因如下:odetocode.com/blogs/scott/archive/2013/06/25/…
    • @Ciccio:我之前没有使用过 $resource,所以我决定尝试一下:github.com/jochenvanwylick/SODemoAngularResourceWebAPI。查看 application.js 以了解我认为 $resource 可以如何用于 WebAPI
    • 谢谢...但最后这个链接在资源方面帮助了我很多...也许它对您也有用..sitepoint.com/creating-crud-app-minutes-angulars-resource
    猜你喜欢
    • 2017-01-17
    • 2014-11-10
    • 2018-05-28
    • 2014-02-21
    • 2013-12-03
    • 2022-12-12
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多