【问题标题】:Call method with Ajax in ASP.NET MVC problem在 ASP.NET MVC 问题中使用 Ajax 调用方法
【发布时间】:2022-01-10 09:18:54
【问题描述】:

我是 Ajax 和 Js 的初学者,所以我希望我的按钮调用我的操作方法,所以这是我的脚本:

<div class="buttons">
    <input type="button" class="btn btn-success" id="PassCode" value="پسورد یکبار مصرف"> 
</div>

$(document).ready(function () {
    $("#$PassCode").click(function (e) {
        var form = this;
        $.ajax({
            url: '@Url.Action("RenderOnetimePassword", "Customer")',
            type: 'POST',
            data: $(form).serialize(),
            beforeSend: function () {
                $("#Password").addClass("loading");
            },
            success: function (response) {
                if (!response.success)
                    showError(response.message, response.captcha_string);
                else if (response.url)
                    window.location.href = response.url;
                else if (response.update_section)
                    $("." + response.update_section.name).html(response.update_section.html);

            },
            complete: function () {
                $("#Password").removeClass("loading");
            },
            error: function (error) {
                alert("@T("Account.ErrorNotification")");
            }
        });
    }
});

在客户控制器中我有这个方法:

public ActionResult RenderOnetimePassword (SMSCodeVerificationModel model)
{
     // some code here
}

我想调用我的操作,但是当我点击我的按钮时,它不起作用。

【问题讨论】:

    标签: jquery ajax asp.net-mvc asp.net-ajax


    【解决方案1】:

    欢迎来到前端世界!

    你需要更多地了解 ajaxjquery 中的函数,所以试试这个

          $(document).on('click', function () {
       $("#PassCode").click(function () {
          
                var form = this;
                $.ajax({
                    cache: false,
                    url:'@Url.Action("RenderOnetimePassword", "Customer")',
                    data: $(form).serialize(),
                    type: 'post',
                    beforeSend: function () {
                        $("#Password").addClass("loading");
                    },
                    success: function (response) {
                        if (!response.success)
                            showError(response.message, response.captcha_string);
                        else if (response.url)
                            window.location.href = response.url;
                        else if (response.update_section)
                            $("." + response.update_section.name).html(response.update_section.html);
    
                    },
                    complete: function () {
                        $("#Password").removeClass("loading");
                    },
                    error: function (error) {
                        alert("@T("Account.ErrorNotification")");
                    }
                });
         });
        });
    

    希望它能解决你的问题:)

    【讨论】:

    • tnx 为你的帮助兄弟
    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 2021-02-06
    • 2014-06-17
    • 1970-01-01
    • 2015-07-19
    • 2018-03-18
    • 1970-01-01
    相关资源
    最近更新 更多