gpt4 book ai didi

javascript - ajax 调用永远不会回发到服务器

转载 作者:行者123 更新时间:2023-12-03 09:33:20 24 4
gpt4 key购买 nike

我正在尝试向服务器发出 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调用中的success函数永远不会被执行

最佳答案

如果您将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com