我在使用 $http.post 在 angularjs 中传递复杂的 json 对象时遇到问题。我不断收到从服务器发回的 400 bad request 错误,表示该请求在语法上不正确。我相信它与数组有关,因为当我不包含它时它会很好地传递。
我正在传递 json。
{
customer: {
firstName: "John",
lastName: "Doe",
street: "1234 South Dr",
city: "Detroit",
state: "MI",
zip: "12345",
phone: "123-321-1234",
email: "[email protected] /cdn-cgi/l/email-protection"
},
order: {
orderDate: "06-16-2015",
registerNum: "1",
transactionNum: "7820",
deliveryStatusID: 1,
notes: "Hold order until July",
items: [
{skuID: "1234568",
skuDescription: "Order item 1",
qty: "4",
itemStatusID: 1,
itemStatusDescription: "Backorder"
},
{skuID: "7387491",
skuDescription: "Order item 2",
qty: "1",
itemStatusID: 1,
itemStatusDescription: "Flagged"
}
]
}
}
角度服务函数
this.addOrder = function(new_order) {
return $http.post(base + "/add", new_order);
};
Spring MVC 控制器方法
@RequestMapping(value="/add", method=RequestMethod.POST)
public void addOrder(@RequestBody CustomerOrder customerOrder) {
System.out.println("----CUSTOMER-INFO----");
System.out.println(customerOrder.getCustomer().getFirstName());
System.out.println(customerOrder.getCustomer().getLastName());
System.out.println("");
System.out.println("----ORDER-INFO----");
System.out.println(customerOrder.getOrder().getOrderID());
System.out.println(customerOrder.getOrder().getOrderDate());
}
该问题似乎仅在我传递 json 中的 items 数组时才会出现。我已经传递了相同的 json 对象,但没有 items 数组,并且它工作正常。 json 的格式以与每当我使用 angularjs 服务方法获取订单时返回的格式相同的格式发送,所以我真的不确定我在哪里出了问题。
如果我需要提供更多代码,请告诉我。我感谢任何帮助我的努力。
谢谢。
Jason