alcatrazbr
5/16/2015 - 3:44 PM

jQuery ajax submit response json

jQuery ajax submit response json

       $("#loginform").submit(function(){             
            $(".input-group").removeClass('has-error');
            // verifica se os campos estão preenchidos
            if (($("#login-password").val() == '') || ($("#login-username").val() == '')) {
                $("#login-alert").text('E-mail e senha são obrigatórios');
                $("#login-alert").removeClass('alert-warning').addClass('alert-danger');
                $("#login-alert").show("slide", {direction:'up'}, 500, function () {                
                    setTimeout(function() { $("#login-alert").hide("clip", {}, 500); }, 3000);
                });
                $(".input-group").addClass('has-error');
                return false;
            }

            $.ajax({ // create an AJAX call...
                data: $(this).serialize(), // get the form data
                type: $(this).attr('method'), // GET or POST
                url: $(this).attr('action'), // the file to call
                success: function(response, status, xhr) { // on success..
                        var Retorno = $.parseJSON(response);
                        $("#login-alert").text(Retorno.Msg);
                        
                        if (Retorno.Status != 'Error') {                            
                            $("#login-alert").removeClass('alert-danger').addClass('alert-success');
                            $("#login-alert").show("slide", {direction:'up'}, 1000, function () {                
                                setTimeout(function() { $("#login-alert").hide("clip", {}, 500); }, 3000);
                                window.location.href = '/cliente/cadastro/';
                            });                            
                        } else {
                            $("#login-alert").removeClass('alert-success').addClass('alert-danger');
                            $("#login-alert").show("slide", {direction:'up'}, 1000, function () {                
                                setTimeout(function() { $("#login-alert").hide("clip", {}, 500); }, 3000);
                            });                            

                        }
                    
                },
                error: function(response, status, xhr){
                        $("#login-alert").text(response.statusText);
                        $("#login-alert").removeClass('alert-success').addClass('alert-danger');
                        $("#login-alert").show("slide", {direction:'up'}, 500, function () {                
                            setTimeout(function(){$("#login-alert").hide("clip", {}, 500);}, 3000);
                        });
                    }
            });

            return false;

       });