2013-11-26 159 views
3

我想通過ajax瀏覽我的請求。Ajax成功函數返回空數據

當用戶通過驗證後它去Ajax的成功功能的其他網址 -

success:function(data,result) 
{ 
window.location.url='@Url.Action("Home","Index"); 
} 

而且我控制器是 -

public void _LoginPartial(LoginModel login) { 
      try { 

       var system = (from u in db.RegisterLedgers 
           where u.EmailID == login.EmailID && u.RegisteredPassword == login.PaSsWoRd 
           select u).FirstOrDefault(); 
       if (system != null) { 

        Session["LoggedInUser"] = system; 
        FormsAuthentication.SetAuthCookie(system.RegisteredName, false); 
       } 
      } 
      catch (Exception ex) { 
       ModelState.AddModelError("", ex); 
      } 
     } 

的Ajax查詢是 -

<script type="text/javascript"> 
    $(function() { 
     $('#Login-button').click(function() { 
      $.ajax({ 
       url: '/loginledger/_loginpartial/', 
       data: $('#form-loginpartial').serialize(), 
       type: 'post', 
       success: function (data, result) { 
        alert(result); 
        alert(data); 
       }, 
       error: function() { 
        alert("Form couldn't be submitted"); 
       } 

      }); 

     }); 

    }); 

</script> 

它顯示ajax查詢是成功的,但數據是em PTY。我在服務器端檢查了這個請求,並從數據庫中取出記錄。

爲什麼我在ajax成功函數中得到空的結果?我在這裏做錯了什麼?

回答

4

我在這裏做錯了什麼?

您的控制器操作似乎不會返回任何內容。這只是一個void。在ASP.NET MVC Controller中,動作必須返回ActionResults。

例如,你可以一個JsonResult

public ActionResult _LoginPartial(LoginModel login) 
{ 
    try 
    { 
     var system = 
      (from u in db.RegisterLedgers 
      where u.EmailID == login.EmailID && u.RegisteredPassword == login.PaSsWoRd 
      select u).FirstOrDefault(); 

     if (system != null) 
     { 
      Session["LoggedInUser"] = system; 
      FormsAuthentication.SetAuthCookie(system.RegisteredName, false); 
      return Json(new { success = true; }); 
     } 
    } 
    catch (Exception ex) 
    { 
     ModelState.AddModelError("", ex); 
    } 

    return Json(new { success = false; }); 
} 

,你可以檢查你的AJAX成功回調:

success: function (data) { 
    alert(data.success); 
} 
3

_LoginPartial動作沒有返回類型(void)。您必須爲_LoginPartial操作設置返回類型,然後返回一些內容。例如return View()return Json()

修訂簽名:

public ActionResult _LoginPartial(LoginModel login) 
{ 
    // also a good idea to check `ModelState.IsValid` 
    if (ModelState.IsValid) 
    { 
     // Go ahead and log them in if everything checks out, then return 
     // your successful response 
     return Json(new { status = 200, msg = "OK" }); 
    } 
    // Erroneous response 
    return Json(new { status = 401, msg = "Unauthorized" }); 
} 

只是例子給出;很明顯,如果你認爲合適的話就實施它。

0

你的行爲應該返回一些東西(推薦的JsonResult)。要做到這一點,請用ActionResult或JsonResult替換返回類型(void),並在函數結束返回Json()之類的東西:

public ActionResult_LoginPartial(LoginModel login) { 
     try { 

      var system = (from u in db.RegisterLedgers 
          where u.EmailID == login.EmailID && u.RegisteredPassword == login.PaSsWoRd 
          select u).FirstOrDefault(); 
      if (system != null) { 

       Session["LoggedInUser"] = system; 
       FormsAuthentication.SetAuthCookie(system.RegisteredName, false); 

      return Json(data = result, message = "suceed" JsonRequestBehavior.AllowGet); 
      } 

      return Json(data=null, message = "system is null" JsonRequestBehavior.AllowGet); 
     } 
     catch (Exception ex) { 
      ModelState.AddModelError("", ex); 
      return Json(data=null, message = "exception" JsonRequestBehavior.AllowGet); 
     } 
    }