jQuery的/ Asp.Net MVC用户名查询 [英] jQuery/Asp.Net mvc username lookup

查看:128
本文介绍了jQuery的/ Asp.Net MVC用户名查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,对我的生活,我不能uderstand为什么follwing code总是返回false?我已经用Firefox调试,看看有什么事情,但功能似乎总是返回false,即使拍摄条件的用户名是0(假),这里是jQuery的code:

OK, for the life of me I can't uderstand why the follwing code is always returning false?? I have debugged with firefox to see what going on, but the function seems to always return false even when the condition username taken is 0(false), here is the jquery code:

function CheckAvailability() {
    showLoader();
    $.post("/Account/CheckUsernameAvailability",
    { userName: $(profile_username).val() },
    function(data) {
        var myObject = eval('(' + data + ')');
        var newid = myObject;
        if (newid == 0) {
            profile_username_error.removeClass("field_not_valid");
            profile_username_error.addClass("field_valid");
            $("#validUserName_msg").html("<font color='green'>Available</font>")
            return true;
        }
        else {
            profile_username_error.addClass("field_not_valid");
            profile_username_error.removeClass("field_valid");
            $("#validUserName_msg").html("<font color='red'>Taken</font>")
            return false;
        }
    });
}

我用/帐号/ CheckUsernameAvailability检查,如果给定的名称是采取与否,它没有采取(0)应该返回true,否则为false。

I'm using the /Account/CheckUsernameAvailability to check if a given name is take or not, it not taken(0) should return true, false otherwise.

推荐答案

您似乎没有被允许它获得响应从动作回电。你需要做的是掀起异步调用,并等待回调返回。

You don't seem to be allowing it to get the response back from the action call. What you need to do is to set off async call and wait for the callback to return.

        $.ajax(
        {
            type: "POST",
            url: "/Account/CheckUsernameAvailability",
            data: "userName=" + $(profile_username).val(),
            dataType: "json",
            success: function(data) { 
                if (data.Success) {
                  // do success stuff
                }
                else {
                 // do failure stuff
                }
            }
        });

这就是所谓的然后可以返回使用JsonResult一些JSON code的操作。

The action which is called then can return some json code using a JsonResult.

   public JsonResult CheckUsernameAvailability(string userName)
   {
      // do some checking with the Membership code

      // this is just a simple class which can set the data and then
      // be passed through to be sent back to the browser as Json
      var result = new UiResult
                   {
                      Message = "Availble",
                      Success = true
                   };

      return Json(result);
   }

希望这有助于。

这篇关于jQuery的/ Asp.Net MVC用户名查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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