Redirect from java script, inside a Json Succces

2

I am trying to validate the login of my application with a POST method, but the problem is that I do not know how to redirect once I get the response from the server.

$.ajax({
    type: "POST",
    url: '@Url.Action("IniciarSesion", "Inicio")',
    content: "application/json; charset=utf-8",
    dataType: "json",
    data: { "Usuario": _usuario, "Clave": _clave },
    success: function (respuesta) {

        if (respuesta.model == '1') {

            //aqui debería redireccionar de alguna manera

        } else if (respuesta.model == '0')
        {
            Alert.render('El usuario ingresado no está registrado');

        } else if (respuesta.model == '2') {

            Alert.render('La clave ingresada es incorrecta');
        }
    },
    error: function (xhr, textStatus, errorThrown) {
        Alert.render('Error: No se pudo cargar método "Validar Usuario"');
    }
});

I work it this way, since I need to indicate information of income errors. That's why I do not use the submit.

    
asked by Bastian Salazar 27.06.2017 в 02:01
source

4 answers

0

The method location.href=" link "; or location.href="tupagina.html" serves, we must consider, the following: location.href="Controller / ActionResult";

$.ajax({
                    type: "POST",
                    url: '@Url.Action("IniciarSesion", "Inicio")',
                    content: "application/json; charset=utf-8",
                    dataType: "json",
                    data: { "Usuario": _usuario, "Clave": _clave },
                    success: function (respuesta) {

                        if (respuesta.model == '1') {
                            location.href = "Home/Inicio/";
                        } else if (respuesta.model == '0')
                        {
                            Alert.render('El usuario ingresado no está registrado');
                        } else if (respuesta.model == '2') {
                            Alert.render('La clave ingresada es incorrecta');
                        }
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        Alert.render('Error: No se pudo cargar método "Validar Usuario"');
                    }
                });
    
answered by 27.06.2017 / 02:24
source
3

What you should do is the following:

Add the following line in the field where you want to redirect:

window.location.href = "urlaredireccionar.html";

You access window and redirect to another window with the desired url.

$.ajax({
    type: "POST",
    url: '@Url.Action("IniciarSesion", "Inicio")',
    content: "application/json; charset=utf-8",
    dataType: "json",
    data: { "Usuario": _usuario, "Clave": _clave },

}).done(function (respuesta){
   if (respuesta.model == '1') {
            //aqui debería redireccionar de alguna manera
            window.location.href = "urlaredireccionar.html";

        } else if (respuesta.model == '0')
        {
            Alert.render('El usuario ingresado no está registrado');

        } else if (respuesta.model == '2') {

            Alert.render('La clave ingresada es incorrecta');
        }
}).fail(function (xhr, textStatus, errorThrown){
    Alert.render('Error: No se pudo cargar método "Validar Usuario"');
});

UPDATE

With the new versions of jQuery some things became obsolete as they are   success, error y complete Now you must use .done, .fail, .always

For now with browsers such as Chrome and Firefox you will not find problems but if you try to execute your code in internet explorer you are more likely to find problems or your script fails and you do not know what happened without seeing the console.

    
answered by 27.06.2017 в 08:11
0
location.href = "https://www.google.com/";

Change google to the page you want to address.

    
answered by 27.06.2017 в 02:09
0

I see that you are not useful location.href try it this way

window.location.replace("http://stackoverflow.com");
    
answered by 27.06.2017 в 02:35