0

I'm trying to redirect my application from ajax call but it isn't working. Inspecting browser's NetWork i could see that the result is 200 - OK but the view isn't changing.

$.ajax({
  type: "GET",
  url: "Index",
  data: { codCliente: codCliente, numProcesso: numProcesso }
});
public ActionResult Index(string codCliente = null, string numProcesso = null)
{
    return View();
}
Glenn Mateus
  • 319
  • 1
  • 2
  • 17
  • To be more specific than simply how to redirect in JavaScript... Note that in your action you are returning an *entire view*, but then not doing anything with it in the client-side code. If you redirect, you're just ignoring that returned content and then requesting it *again* after the redirect. If you're going to redirect after the AJAX call and ignore anything returned from it, then don't bother returning anything from it. It's just a waste of bandwidth at that point. Only return to the client what the client needs. – David Oct 25 '18 at 19:58
  • It's also possible that you actually don't want to "redirect" or even use AJAX at all. If you just want to post data to the server and display the response, just use a normal HTML form. No need for JavaScript at all. – David Oct 25 '18 at 20:00
  • I'm doing in that way because I need to get the parameters to select a user from database and show all data searched – Glenn Mateus Oct 25 '18 at 20:09
  • Take a step back and determine what actually needs to happen here. Do you need to send data to the server to perform some operation, and then following that operation redirect the user to a different page? Doesn't seem that way, since your server-side code isn't performing any operation. Or do you need to simply post form values to the server and render the result as a new page? If so then what you're looking for isn't AJAX, what you're looking for is simply a form post. – David Oct 25 '18 at 20:13
  • What I pretend to do is: when user performs a click on a button of a datatable, get some values from that datatable and then redirect to another page where I'll select data from database, using collected values, and at the end show to the user a new form filled with database results – Glenn Mateus Oct 25 '18 at 20:30
  • Then just use a standard form post. You're over-engineering this by trying to use AJAX or even JavaScript at all. What you're asking in the question is how to redirect the user in JavaScript, but that's not actually what you need. Just use an HTML form to post to the controller action. – David Oct 25 '18 at 20:32
  • Even if the action is from a datatable button? – Glenn Mateus Oct 25 '18 at 20:33
  • It doesn’t (shouldn’t) matter what the action is from. The operation you want to perform, from your description, is to direct the user to make a request with some parameters. Not AJAX. You can use JavaScript if you want, but you don’t want AJAX. – David Oct 25 '18 at 20:56

0 Answers0