无法使用jquery-ajax将json数组发送到web api

时间:2018-06-12 12:57:25

标签: jquery asp.net-web-api asp.net-ajax

我的web api方法没有通过ajax调用接收任何数据。 实际上我发送的对象数组是这样的:

 [{"Id":1,"columnName":"Name","onPage":true,"onReport":false},{"Id":2,"columnName":"Email","onPage":true,"onReport":false},{"Id":3,"columnName":"Password","onPage":false,"onReport":true},{"Id":4,"columnName":"Country","onPage":false,"onReport":true},{"Id":5,"columnName":"State","onPage":false,"onReport":true},{"Id":6,"columnName":"filename","onPage":false,"onReport":false},{"Id":7,"columnName":"Salary","onPage":false,"onReport":false}]

   $(document).on('click', '#btnSave', function () {
    console.log(expressionValue);
    var obj = JSON.stringify(expressionValue);
    console.log(obj );
    $.ajax({
        type: "POST",
        url: "/api/test/saveSettings",
        dataType:"JSON",
        data: {expressionValue : JSON.stringify(expressionValue)},
        success: function () {
            alert("Settings updated successfully");
        }
    });
});

Web Api方法:

        [HttpPost]
        public void saveSettings( setting s)
        {

        }

我的模型看起来像这样:

public class setting
{
    public int Id { get; set; }
    public string columnName { get; set; }
    public Nullable<bool> onPage { get; set; }
    public Nullable<bool> onReport { get; set; }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

$(document).on('click', '#btnSave', function () {
    //console.log(expressionValue);
    //var obj = JSON.stringify(expressionValue);
    //console.log(obj );
    $.ajax({
        type: "POST",
        url: "@Url.Action("saveSettings","test")",
        data: {s: JSON.stringify(expressionValue)},
        success: function () {
            alert("Settings updated successfully");
        }
    });
});