Ajax success function returns empty data
ajax, asp.net-mvc-4, c#, jquery
Solution
What did I do wrong here?
Your controller action doesn't seem to return anything. It's just a `void`. In ASP.NET MVC Controller actions must return ActionResults.
For example you could a 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; });
}
which you could check in your AJAX success callback:
success: function (data) {
alert(data.success);
}
Problem
I am trying to navigate my request through ajax. When a user is authenticated it goes to another url on ajax success function - ``` success:function(data,result) { window.location.url='@Url.Action("Home","Index"); } ``` And my controller is- ``` 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 query is- ``` <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> ``` It shows that ajax query is successful but the data comes empty. I checked this request on server side and it fetches out record from database. Why I am getting empty result in ajax success function? What did I do wrong here?