jQuery AJAX post数据在c#web api控制器中为null [英] jQuery AJAX post data is null in c# web api controller
本文介绍了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') },
});
这篇关于jQuery AJAX post数据在c#web api控制器中为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文