ASP.NET MVC jQuery的AJAX调用处理法太晚了? [英] ASP.NET MVC jQuery AJAX Calls Action Method too late?

查看:143
本文介绍了ASP.NET MVC jQuery的AJAX调用处理法太晚了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些jQuery code在正在为一个操作方法,我有我的控制器Ajax调用外部JavaScript文件。在code在外部JavaScript文件如下。该操作方法被称为最终,但是它不被调用的时间。当我设置的操作方法一个破发点,它没有被调用,直到后提交按钮单击返回false。当EmailExist函数返回的isValid的isValid是不确定的。

I have some jQuery code in an external Javascript file that is making an ajax call to an action method I have in my controller. The code in the external Javascript file is below. The action method is being called eventually, however it is not being called in time. When I set a break point in the action method, it is not being called until after the submit button click returns false. When the EmailExist function returns "isvalid", isvalid is undefined.

因此​​,操作方法它不是一个问题被称为最终,它只是不叫无论出于何种原因EmailExist始终返回未定义和MVC操作方法不叫,直到按一下按钮返回。有什么想法?

So its not a matter of the action method being called eventually, it just is not called for whatever reason EmailExist always returns undefined and the MVC Action method is not called until the button click is returned. Any thoughts?

操作方法:

        [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult EmailExist(string email)
    {
        Account account = _generalService.GetAccountByEmail(email);

        bool exist = false;
        if (account != null)
            exist = true;

            return Json(new { indb = exist });
    }

JavaScript的:

Javascript:

$(document).ready(function() {
$("input[type=submit]").click(function() {

    var valid = true;

    // email - check required and valid

    var email = $("input[name='email']");
    var emailtrim = jQuery.trim(email.val());

    // email exists in db ajax request
    if (EmailExist(emailtrim)) {
        valid = false;
    }

    return valid;
})

function EmailExist(emailval) {

    var isvalid;

    // hit db
    $.ajax(
    {
    type: "POST",
    url: "/Account/EmailExist",
    data: { 'email': emailval },
    success: function(result) {

        if (result.indb) {
            isvalid = true;

        }
    },
    error: function(error) {
        alert(error);
    }
});

    return isvalid;
}

});

推荐答案

它看起来像你想的要求是同步的。为了让这种情况发生,你需要设置的要求,假async选项。默认是有要求,让你从Ajax调用返回请求完成之前是异步的。或者,你可以重写code,以便成功回调实际上做的工作。后者更惯用对AJAX - 但有时可能更复杂的

It looks like you want the request to be synchronous. To get this to happen you need to set the async option on the request to false. The default is to have the request be asynchronous so that you return from the ajax call before the request is complete. Alternatively, you can rewrite the code so that the success callback actually does the work. The latter is more idiomatic for AJAX -- but can sometimes be more complex.

$.ajax(
    {
    type: "POST",
    async: false,              // <- note addition
    url: "/Account/EmailExist",
    data: { 'email': emailval },
    success: function(result) {

        if (result.indb) {
            isvalid = true;

        }
    },
    error: function(error) {
        alert(error);
    }
});

$("input[type=submit]").click(function() {

    // email - check required and valid

    var form = $(this).parent('form');
    var email = $("input[name='email']");
    var emailtrim = jQuery.trim(email.val());

    SubmitIfNoEmailExists(form,emailtrim);

    return false;
});

function SubmitIfNoEmailExists(form,emailval) {

    // hit db
    $.ajax({
        type: "POST",
        url: "/Account/EmailExist",
        data: { 'email': emailval },
        success: function(result) {
            if (!result.indb) {
               form.submit();
            }
        },
        error: function(error) {
            alert(error);
        }
    });
}

修改:根据您的意见,你可能也想看看使用jQuery的验证插件作为解决所有的验证需求。

EDIT: Based on your comments, you may want to also look at using the jQuery validation plugin as a solution to all of your validation needs.

这篇关于ASP.NET MVC jQuery的AJAX调用处理法太晚了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆