value不返回null但它在null值的条件内

时间:2017-04-05 03:24:40

标签: angularjs json ionic-framework

我有一个返回json字符串的服务:{" IsExisting":" true"}如果在数据库中验证了用户。但是,如果我输入正确的凭证,代码将进入响应的空条件而不是真实条件。可能是什么问题呢?

这是我的controller.js代码:

$scope.enterlogin = function(usern,pass)
{
    console.log('username is = ' + usern);
    loginService.getUser(usern,pass).then(function(response){
      console.log('user is = ' + response);
      var obj = JSON.parse(response);
      console.log('object is ' + obj['IsExisting']  );
      if(obj['IsExisiting'] == null)
      {
          alert('Account does not exist. Please check your credentials.');
      }
      else
      {
          if(localStorage['firstTimeLoad']!='TRUE')
        {
            $scope.hide();
            localStorage['firstTimeLoad']='TRUE';
            $state.go('helpExtra'); 
        }
        else
        {
            $scope.hide();
            $state.go('menu.mainMenu');
        }
      }
    });
}

这是我的服务代码:

//angularSoap Login
.factory('loginService', ['$soap', function($soap){
var base_url = "http://localhost/UserService3/WebService1.asmx";

return {
    getUser: function(usern,pass){
        console.log('code side usern is = ' + usern + "" + pass);
        return $soap.post(base_url, "getUserbyUsername", { uname: usern, passw: pass});
    }
}
}])

这是我的网络服务代码:

    [WebMethod]
    public string getUserbyUsername(string uname, string passw)
    {
        string cs = "Data Source =.; Initial Catalog = UsersDB; Integrated Security = True";
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("spGetUserByUsername", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter = new SqlParameter(@"Username", uname);
            SqlParameter parameter2 = new SqlParameter(@"Password", passw);
            cmd.Parameters.Add(parameter);
            cmd.Parameters.Add(parameter2);
            User user = new User();
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                // user.Username = reader["Username"].ToString();
                //  user.Password = reader["Password"].ToString();
                user.IsExisting = reader["IsExisting"].ToString();
            }
            con.Close();

            return new JavaScriptSerializer().Serialize(user);
        }
    }

1 个答案:

答案 0 :(得分:0)

问题在于您尝试在对象上访问的属性的拼写。

应该是

 if(obj['IsExisting'] == null)
 {
     alert('Account does not exist. Please check your credentials.');
 }

而不是

 if(obj['IsExisiting'] == null)