Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /UsersPage/undefined
i have this ajax script which i supposedly redirect the user to the specified url:
$.ajax({
url: '@Url.Action("UsersHome", "UsersPage")',
type: 'POST',
data: {
chosenMood: chosenMood
},
success: function(response) {
// Handle the success response
console.log('Success:', response);
// Redirect to the received URL
window.location.href = response.redirectUrl;
},
error: function(xhr, status, error) {
// Handle errors, if any
console.error('Error:', error);
// Optionally, you can display an error message to the user
alert('An error occurred. Please try again later.');
}
});
and this is the controller or the target page:
public ActionResult UsersHome()
{
if (User.Identity.IsAuthenticated)
{
//Session["ChosenMood"] = chosenMood;
var redirectUrl = Url.Action("UsersHome", "UsersPage");
//return Json(new { redirectUrl }, JsonRequestBehavior.AllowGet);
return View();
} else
{
return RedirectToAction("../Home/Index");
}
}
There are a few problems in this code.
If You are making an ajax request to action method in controller , if it is returning the view you'll get html code in ajax response. Hence in your case, you need to send Json with redirect URL
Also after sending redirectUrl in your json, it will throw 404 cause in success function window.location.href will call GET action method Url.Action("UsersHome", "UsersPage") ,which actually is POST in your controller (assumption).
Modifying your code like this will work
Controller Code
Ajax Code
NOTE Request.IsAjaxRequest() will check for ajax request and for first time it will return Json . And On window.location.href it will render View.