通过ajax和JSON将数据保存到可手持的Web API
我通过ajax将我的可手持数据发送到WebAPI POST方法,但是数据在WebAPI端显示为空白。我还使用fiddler查看发送的内容,它显示了我的表数据,但是由于某种原因,我猜它没有被反序列化。这是我的密码通过ajax和JSON将数据保存到可手持的Web API,ajax,json,post,asp.net-web-api,handsontable,Ajax,Json,Post,Asp.net Web Api,Handsontable,我通过ajax将我的可手持数据发送到WebAPI POST方法,但是数据在WebAPI端显示为空白。我还使用fiddler查看发送的内容,它显示了我的表数据,但是由于某种原因,我猜它没有被反序列化。这是我的密码 var $container = $("#example"); var $parent = $container.parent(); $container.handsontable({ data: createBigData(),
var $container = $("#example");
var $parent = $container.parent();
$container.handsontable({
data: createBigData(),
colWidths: [150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150], //can also be a number or a function
rowHeaders: true,
colHeaders: true,
minSpareRows: 1,
stretchH: 'all',
contextMenu: true,
colHeaders: ['Stage Number', 'Horizon Name', 'TVD of top of horizon (feet)', 'Measured depth of top of horizon (feet)',
'Pool Code', 'Measured depth on well bore-Start (Feet)', 'Measured depth on well bore-End (Feet)', 'True vertical depth on well bore-Start (feet)',
'True vertical depth on well bore-End (feet)', 'Length of stimulation (feet)', 'Height of stimulation (feet)', 'Direction of stimulation',
'Volume of Well Stimulation fluid for stage (BBLS)'],
var handsontable = $container.data('handsontable');
$parent.find('button[name=save]').click(function () {
var myData = { data: handsontable.getData() }
$.ajax({
url: "/api/values",
data: JSON.stringify(myData), //returns all cells' data
dataType: 'json',
type: 'POST',
contentType: "application/json; charset=utf-8",
success: function (res) {
if (res.result === 'ok') {
$console.text('Data saved');
}
else {
$console.text('Save error');
}
}
});
});
这是我的WebAPI方法
// POST api/values
[HttpPost]
public void Post([FromUri] List<long> data)
{
DataTable table = new DataTable();
foreach(var value in data)
{
//
}
}
请帮忙!我所要做的就是将我的可手持数据发送到web api,这样我就可以使用这些数据插入我的数据库。您的web api方法中有一个简单的错误 您可以这样做,即告诉WebAPI数据应该位于文章的URI/URL中:
public void Post([FromUri] List<long> data)
public void Post([FromBody] List<long> data)
如果您实际发送的是显示的示例数据,那么您需要修改代码以理解对象,而不是期望一个数字列表
public void Post([FromBody] List<long> data)
[1,2,3,4,5,6,7,8,9,10]