2011-09-26 61 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; 
         } 
        });