jQuery AJAX post数据在c#web api控制器中为null
问题描述:
我使用以下电话将数据发送到 c#web api控制器
。
I am sending data to a c# web api controller
by using the following call.
$.ajax({
type: "POST",
url: "menuApi/menu/Cost",
data: JSON.stringify(order),
contentType: "application/json",
success: function (data) { window.alert('done')},
dataType: 'json'
});
服务器端的 c#controller
就像这个:
public string Cost([FromBody] string order)
{
var sOrder = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(order);
return "";
}
Javascript中的订单对象是一个具有嵌套数组和属性的复杂对象。我将数据作为null。我不知道如何通过ajax调用获得订单。
The order object in Javascript is a complex object with nested arrays and properties. I am getting data as null. I am not sure how can I get the order sent through the ajax call.
编辑:这是我的订单对象
This is my order object
var order = {
name:"",
id:"",
cost:"",
details: {
sItem:[{name:"",cost:""}],
dItem:[{name:"",cost:"", components:[{name:"",quantity:""}]}]
}
}
答
知道了,您需要将ajax请求作为带有空参数/表单字段名称的表单数据发布,如下所示:
Got it, you need to post the ajax request as form data with empty parameter/form field name like this:
var order = {
name: "",
id: "",
cost: "",
details: {
sItem: [{ name: "", cost: "" }],
dItem: [{ name: "", cost: "", components: [{ name: "", quantity: "" }] }]
}
};
$.ajax({
type: "POST",
url: "api/Values",
data: {'': JSON.stringify(order)} ,
success: function (data) { window.alert('done') },
});