我正在尝试向服务器发出ajax调用,以将新数据保存到数据库中:
这是我的代码:

$.ajax({
    type: 'POST',
    url: '@Url.Action("SaveNewApplication")',
    contentType: 'application/json; charset=utf-8',
    data: {
        appName: newAppName,
        appDesc: newAppDesc,
        expire: newDaysToExpire,
        displayNote: newDspNote,
        adminRole: newAdminRole,
        defualtRole: newDefaultRole,
        active: newIsActive
    },
    dataType: '',
    success: function () {
        alert("new data has been saved");
    },
    error: function () {
        alert("Error happened whiles saving the new application data");
    }
});


我在“ SaveNewApplication”函数中设置了一个断点,但是断点从未被击中!
这是“ SaveNewApplication”函数:

    [HttpPost]
    [OutputCache(Duration = 0)]
    public void SaveNewApplication(string appName, string appDesc, int expire, string displayNote, string adminRole,
       string defualtRole, bool active)
    {

    }


永远不会执行ajax调用中的成功函数

最佳答案

如果将contentType指定为json,只需确保您实际上正在发送JSON。 JSON.stringify() converts javascript values into JSON strings

$.ajax({
    type: 'POST',
    url: '@Url.Action("SaveNewApplication")',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({
        appName: newAppName,
        appDesc: newAppDesc,
        expire: newDaysToExpire,
        displayNote: newDspNote,
        adminRole: newAdminRole,
        defualtRole: newDefaultRole,
        active: newIsActive
    }),
    dataType: '',
    success: function () {
        alert("new data has been saved");
    },
    error: function () {
        alert("Error happened whiles saving the new application data");
    }
});

关于javascript - ajax调用永远不会回发到服务器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31417421/

10-12 16:56
查看更多