i have been facing an issue in my loginscreen of my application which is developed with mvc3 and jquery, as i am writing the code for checking login credentials in controller and and that controller event is firing by clciking the buttoon click which is developed using jquery. i would like to navigate someo other page('/Customer/CollaborationPortal') when the login credentials are correct, else i want display message box stating that "credentials are wrong" and this is my code.
$("#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();
}
and also it is not entering into "success" and "error" code.
Thanks in advance.
you have specified the error callback inside success callback, move it outside like:
$.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");
}
});
You have error function inside of success function....
This it's what you must have:
$.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");
}
});
If I were you, I'd return a JSON object to the client like this:
Success:
{
'error' : false,
'redirect' : '/Customer/CollaborationPortal',
'message' : ''
}
Error:
{
'error' : true,
'redirect' : false,
'message' : 'Please do this or that'
}
My jQuery would be:
$.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;
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.