【问题标题】:How to make POST request in my app如何在我的应用程序中发出 POST 请求
【发布时间】:2015-05-05 04:57:19
【问题描述】:

我正在尝试使用 $resource 发出发布请求。

在我的应用中,

angular.module('myApp').factory('Product', ['$resource',
    function ($resource) {
        return $resource( '/api/product/:id', {
                id: '@id'
            }, {
                   'save': {
                    method: 'POST',
                    isArray: true,
                    url: '/api/product/:id/addProduct' 
                    //different url for adding a product.
                ,}
            }
        );
    }
]);

在我的控制器中

Product.save({
                name:'test name',
                quantity:'5'
            })

它通过像这样的url发出请求

/api/product/addProduct    //without id

但如果我这样做了

Product.save({
                id:'12345',
                name:'test name',
                quantity:'5'
            })

请求 url 正确,如/api/product/12345/addProduct 但是有效载荷变成了

id:'12345',
name:'test name',
quantity:'5'

服务器不会将 id 参数作为有效负载。 我不确定如何提出发布请求。任何人都可以帮助我吗?非常感谢!

【问题讨论】:

    标签: javascript angularjs resources xmlhttprequest


    【解决方案1】:

    将 'id' 对象作为保存方法中的第一个参数传递:

    Product.save({"id":"12345"}, {
                        name:'test name',
                        quantity:'5'
                    }) 
    

    工作代码:http://jsfiddle.net/7rx5fqx2/1/

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 2020-04-15
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多