我正在尝试使用 OData 将数据发送到 Web API。但是接收到的实体总是空的。
我有这个使用 JQuery 的电话:
$.ajax({
url: "/odata/Products",
type: "POST",
//contentType: "application/json;odata=verbose",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(productToAdd),
dataType: "json",
success: function () {
alert("Server call has been OK!!");
},
error: function () {
alert("Server call has been failed!!");
}
});
productToAdd 对象如下:
{"name":"Apple","description":"Good!!","price":"12.3","category":"Fruit"}
我的控制器是:
public class ProductsController : EntitySetController<Product, string>
{
private IProductService _prodSvc;
public ProductsController(IProductService prodSvc)
{
_prodSvc = prodSvc;
}
[Queryable]
public override IQueryable<Product> Get()
{
return _prodSvc.GetAllProducts();
}
protected override Product CreateEntity(Product product)
{
var addedProduct = _prodSvc.AddProduct(product);
return addedProduct;
}
}
我也尝试使用 datajs-1.10.js 框架,但我得到了相同的结果。
OData.request(
{
requestUri: "/odata/Products",
method: "POST",
data: productToAdd
},
function (data) {
alert("Server call has been OK!!");
}
);
我认为反序列化 JSON 对象时出了点问题,但我不知道是什么。你可以帮帮我吗?
谢谢!!