angularjs $ http发布数据对象

时间:2017-01-09 21:33:06

标签: angularjs http asp.net-web-api

在angularjs中使用$ http方法如何将以下数据传递给Web API?

我有以下格式的数据。我想将产品和订单传递给web api。

var product : {
  productID : 1234,
  productName: Amamzone Echo,
  DateofPurchase : 12/12/2016
 }

var orders = [   
   { productID : 1234,
     CustomerBought: Nancy,
     CustomerPaid : 323.12   
   },

  { productID : 1234 ,
     CustomerBought: John,
     CustomerPaid : 123.12   
  },
  { productID : 1234,
     CustomerBought: Mark,
     CustomerPaid : 323.12   
  },
];

2 个答案:

答案 0 :(得分:1)

你可以做一个简单的$ http.post,如:

var orders = [
  { 
    productID : 1234, 
    CustomerBought: Nancy, 
    CustomerPaid : 323.12
  },
  { 
    productID : 1234, 
    CustomerBought: John,
    CustomerPaid : 123.12
  },
  { 
    productID : 1234,
    CustomerBought: Mark,
    CustomerPaid : 323.12
  }, 
];

$http.post('http://someAPI/db1/', orders)
  .then(
    function(response) {
      // success callback
      console.log("Sent Orders Successfully!")
    },
    function(response) {
      // failure call back
       console.log("Error while sending orders")
    });

答案 1 :(得分:0)

您需要先修复内容:

var product = {
    productID : 1234,
    productName: 'Amamzone Echo',
    DateofPurchase : '12/12/2016'
};
var orders = [{
    productID : 1234,
    CustomerBought: 'Nancy',
    CustomerPaid : 323.12
}, {
    productID : 1234 ,
    CustomerBought: 'John',
    CustomerPaid : 123.12
}, {
    productID : 1234,
    CustomerBought: 'Mark',
    CustomerPaid : 323.12
}];

然后使用 $ http

将它们发送到对象中
$http.post('/your/url', { product: product, orders: orders }).then(
    function (response) {
        // success callback code
    },
    function (error) {
        // failure callback code
    }
);