gpt4 book ai didi

c# - ajax 调用 c# mvc 发送 post 和 get

转载 作者:行者123 更新时间:2023-11-30 13:00:55 28 4
gpt4 key购买 nike

我研究了几种进行 ajax 调用的解决方案,但由于没有在任何地方提到这个问题,我觉得它可能是我正在使用的环境所特有的。我的 Controller :

    [HttpPost]
public ActionResult ChangeDefualtCC(string a)
{

return Json("ok");
}
[HttpGet]
public ActionResult ChangeDefualtCC()
{

return Json("ok");
}

JS:

    $("nevermind").change(function () {

$.ajax({
type: "POST",
url: "/Account/ChangeDefualtCC",
dataType: "json",
data: {
a: "A"
},
success: function (data) { console.log(data)},
error: function (data) { console.log("error");}
});
});

Controller 代码永远不会被命中,这是我在 ajax 调用后在 chrome 中看到的:

编辑 2:页面点击 [HttpGet] 方法。

chrome network tab

编辑:我也标记了Ektron,因为它在项目中使用,并且它可能影响调用。

我的路线: mvc routes

更新:我尝试过使用 Get 和 Post,并且还返回到我所在的 View ,我每次都得到 302。

有什么想法吗?

最佳答案

看起来它找到了“get”,因为您在该调用中没有参数。我认为您可能缺少 ajax 调用中的内容类型,因此模型联编程序无法将您的帖子内容解析为参数。

$.ajax({
type: "POST",
url: "/Account/ChangeDefualtCC",
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: {
a: "A"
},
success: function (data) { console.log(data)},
error: function (data) { console.log("error");}
});

关于c# - ajax 调用 c# mvc 发送 post 和 get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21316752/

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