使用Handsontable到Web API通过ajax和JSON保存数据

时间:2014-01-13 20:16:13

标签: ajax json post asp.net-web-api handsontable

我通过ajax将我的Handsontable数据发送到Web API POST方法,但是数据在web api端显示为空白。我也用fiddler查看发送的内容并显示我的表格数据,但是由于某种原因它没有被反序列化我猜。继承我的代码

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');
                    }
                }
            });
        });

并且继承了我的Web API方法

 // POST api/values
    [HttpPost]
    public void Post([FromUri] List<long> data)
    {
        DataTable table = new DataTable();

        foreach(var value in data)
        {
            //
        }

    }

当我调试并单步执行代码时,数据根本不包含任何数据!它的空白!即使fiddler将此作为我的JSON数据显示:(不注意值,我只是随机输入到单元格中)

{"data":[["sfgsfs","dfsadfsdf","sdfsdf","fsdf","sdfsdf","sdfsfd"," "," "," "," "," "," "," "],[" ","fsdf"," ","sdfsd"," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],["sdf"," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," ","fsdf"," "," "," "," "," "," "," "],[" "," ","sdfsd"," "," "," "," "," "," "," "," "," "," "],[" ","sdf"," "," "," ","sdf"," "," "," "," "," "," "," "],[" "," "," "," ","sdf"," "," "," "," "," "," "," "," "],[" "," "," ","sdf","sdf"," "," "," "," "," "," "," "," "],["sdfsd"," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[" "," "," "," "," "," "," "," "," "," "," "," "," "],[null,null,null,null,null,null,null,null,null,null,null,null,null]]}

请帮忙!我想做的就是将我的Handsontable数据发送到web api,这样我就可以使用这些数据插入到我的数据库中。

1 个答案:

答案 0 :(得分:0)

您的WebAPI方法中有一个简单的错误。

你有这个,它告诉WebAPI期望数据在POST的URI / URL中:

public void Post([FromUri] List<long> data)

应该是这样,因为JS将数据放在POST的主体中:

public void Post([FromBody] List<long> data)

只要您发送的JSON看起来像这样:

[1,2,3,4,5,6,7,8,9,10]

如果您实际上是在发送您显示的示例数据,那么您需要修改代码以理解该对象,而不是期待一个数字列表。