0

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");
    }
}
1
  • 1
    You have client-side code in JavaScript and server-side code in C#. Your first step in troubleshooting the issue is to figure out where the problem originates. Does the JavaScript send the wrong HTTP request, or does the server handle a correct request in the wrong way? You may want to use your browser's F12/dev tools to see the request that the script sends, and then a tool like curl to reproduce the HTTP request independently of the client-side script. Once you've pinpointed the problem, you may already have solved it. Otherwise, please edit the question with what you've learned. Commented Mar 24, 2024 at 8:03

1 Answer 1

0

There are a few problems in this code.

  1. 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

  2. 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

[HttpGet]
    public ActionResult UsersHome()
    {
        bool auth = true;
        var isAjax = Request.IsAjaxRequest();
        if (auth)
        {
            //Session["ChosenMood"] = chosenMood;
            var redirectUrl = Url.Action("UsersHome", "UsersPage");
            if(isAjax)
                return Json(new { redirectUrl=redirectUrl }, JsonRequestBehavior.AllowGet);

            return View();
        }
        else
        {
                return Json(new { redirectUrl= "../Home/Index" });
        }
    }

Ajax Code

 $.ajax({
url: '@Url.Action("UsersHome", "UsersPage")',
type: 'GET',
        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.');
}
});

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.