2011-09-26 59 views
0

我一直在我的应用程序的登录画面中使用mvc3和jquery开发时遇到问题,因为我正在编写用于在控制器中检查登录凭据的代码以及该控制器事件通过使用jquery开发的buttoon点击来触发。当登录凭证正确时,我想浏览其他页面('/ Customer/CollaborationPortal'),否则我想显示消息框,指出“凭据错误”,这是我的代码。RedirectToAction不工作,而mvc3表单与jquery的身份验证

jQuery代码

$("#btnGo").click(function (e) { 
        var RegData = getRegData(); 
       if (RegData === null) { 
        console.log("Specify Data!"); 
        return; 
       } 

       $.ajax(
         { 
          url: '/Registration/IsLoginExsit', 
          type: 'POST', 
          dataType: 'json', 
          data: JSON.stringify(RegData), 
          contentType: 'application/json; charset=utf-8', 
          success: function (response) { 
           //window.location.href('/Customer/CollaborationPortal'); 
            Console.Log("success"); 
           error: function() { 
            //aler('Login error'); 
            Console.Log("error"); 
           } 
          } 



         }); 
      }); 
      function getRegData() { 

       var UserName = $("#txtUserName").val(); 
       var Password = $("#txtPassword").val(); 
       return { "UserName": UserName, "Password": Password }; 
      } 

     }); 

控制器代码

public ActionResult IsLoginExsit(BAMasterCustomerDO loginData) 
{ 
    if (!string.IsNullOrEmpty(loginData.UserName) && !string.IsNullOrEmpty (loginData.Password)) 
     { 

     bool result = Businesss.Factory.BusinessFactory.GetRegistration().IsLoginExist(loginData.UserName, loginData.Password); 
       if (result) 
       { 
         System.Web.HttpContext.Current.Session["UserName"]=loginData.UserName; 
         return RedirectToAction("/Customer/CollaborationPortal"); 


        } 
        else 
        {     

         ViewData["message"] = "Registration failed"; 
         return View(); 
        } 
     } 

     return View(); 

    } 

并且还没有进入 “成功” 和 “错误” 的代码。

在此先感谢。

回答

0

您指定success回调内部错误回调,向外移动的像:

$.ajax(
{ 
url: '/Registration/IsLoginExsit', 
type: 'POST', 
dataType: 'json', 
data: JSON.stringify(RegData), 
contentType: 'application/json; charset=utf-8', 
success: function (response) { 
//window.location.href('/Customer/CollaborationPortal'); 
console.log("success"); 
}, 
error: function() { 
//alert('Login error'); 
console.log("error"); 
} 
}); 
0

你有成功函数内的误差函数....

这是你必须拥有的一切:

$.ajax(
    { 
    url: '/Registration/IsLoginExsit', 
    type: 'POST', 
    dataType: 'json', 
    data: JSON.stringify(RegData), 
    contentType: 'application/json; charset=utf-8', 
    success: function (response) { 
     //window.location.href('/Customer/CollaborationPortal'); 
     Console.Log("success"); 
    }, 
    error: function() { 
     //aler('Login error'); 
     Console.Log("error"); 
    } 
}); 
0

如果我是你,我会一个JSON对象返回给客户端这样的:

次成功:

{ 
    'error' : false, 
    'redirect' : '/Customer/CollaborationPortal', 
    'message' : '' 
} 

错误:

{ 
    'error' : true, 
    'redirect' : false, 
    'message' : 'Please do this or that' 
} 

我的jQuery的是:

  $.ajax(
        { 
         url: '/Registration/IsLoginExsit', 
         type: 'POST', 
         dataType: 'json', 
         data: JSON.stringify(RegData), 
         contentType: 'application/json; charset=utf-8', 
         success: function (response) { 
          if (response.error) 
          { 
           alert(response.message); 
           return; 
          } 
          window.location.pathname = response.redirect; 
         } 
        });