简体   繁体   English

AJAX到控制器密码确认

[英]AJAX to Controller Password Confirmation

I am newer to both AJAX and MVC and I am trying to make a confirmation interaction. 我对AJAX和MVC都比较陌生,我正在尝试进行确认互动。

What I am trying to do is display a box, the user will enter a value and click submit, and then box will AJAX POST (I think that is what I am supposed to use) to the Controller and return a true or false. 我想做的是显示一个框,用户将输入一个值,然后单击Submit,然后该框将AJAX POST(我认为这是我应该使用的)发送到Controller并返回true或false。 I cannot seem to figure out how to make this interaction work however. 我似乎无法弄清楚如何使这种交互有效。

If I run debug I can see my value getting passed into the Controller, but it ignores my if statement to check if the string was correct. 如果我运行调试,我可以看到我的值被传递到Controller中,但是它将忽略我的if语句来检查字符串是否正确。 Beyond that, I am not sure how to return a true/false and deal with it in my AJAX. 除此之外,我不确定如何在我的AJAX中返回true / false并进行处理。

Here is my "best guess" so far. 到目前为止,这是我的“最佳猜测”。

AJAX/JS AJAX / JS

<script type="text/javascript">
    function preloadFunc() {
        var prompting = prompt("Please enter your password", "****");

        if (prompting != null && prompting != null) {
            $.ajax({
                url: '/Admin/magicCheck',
                type: 'POST',
                data: {
                    'magic': prompting
                },
                success: function () {
                    //let page load
                },
                error: function () {
                    preloadFunc(); //re-ask
                }
            });
        }
        else {
            window.location.replace("google.com"); //Pressing Cancel
        }
    }
    window.onpaint = preloadFunc();
</script>

Controller 调节器

    [HttpPost]
    public ActionResult magicCheck(string magic)
    {
        bool success = false;
        if (magic == "poof")
        {
            success = true;
        }
        else
        {
            success = false;
        }
        return RedirectToAction("Index");
    }

All help greatly appreciated as I am new to this! 感谢所有的帮助,因为我是新来的!

Read all the comments below your post as there are many issues here, but as far as comminication AJAX and returning: 阅读本文下方的所有评论,因为这里有很多问题,但要尽量减少AJAX和返回:

Change the controller to return JSON, like this: 更改控制器以返回JSON,如下所示:

[HttpPost]
public ActionResult magicCheck(string magic)
{
    bool success = false;
    if (magic == "poof")
    {
        success = true;
    }
    else
    {
        success = false;
    }
    return Json( new { Success = success });
}

Now that the controller is returning JSON you need to change your js to handle it: 现在控制器返回了JSON,您需要更改js来处理它:

$.ajax({
            url: '/Admin/magicCheck',
            type: 'POST',
            data: 'magic=' + prompting,   //<-- you didn't need to send JSON for this, you'd have to change your controller if you are going to send a JSON object also... you just need this for now
            success: function (resp) {
                //let page load
                 if ( resp.Success ){
                 // success...
                 }
                 else{
                  //fail
                 }
            },
            error: function () {
                preloadFunc(); //re-ask
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM