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

标签 c# ajax asp.net-mvc ektron

我研究了几种进行 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/

相关文章:

asp.net-mvc - Unity IoC 和 MVC 3 Beta - 将 IRepository 传递给 Controller ​​构造函数

c# - 在 C# 中处理日期

c# - <BitmapImage>.SetSource() 中的 OutOfMemory 异常

c# - 浏览器选项卡上的 Blazor 服务器端 Fluxor 调度操作关闭

javascript - Google 托管库不必要地使用缓存断路器

asp.net-mvc - 保留对包含 MVC3 Controller 的 DLL 项目的工具支持

c# - MVC IdentityServer Db 空用户表/错误播种

c# - 通过引用检索属性

asp.net - 对 .net Web 服务(asmx、wcf 等)进行 AJAX (jquery) 调用时的授权、身份验证,我需要了解什么?

javascript - 如何获取返回servlet给ajax的特定字段?