I currently wrote a quick workaround for what i want done, but i would
like to see how other people would go about doing this. It is simply
an ajax login form, but there is a caveat. There can be two http
responses from the server: 200 OK and 302 Redirect. The 200 codes are
just notifications that either the login was invalid or the user
account has been deactivated, so i simply apply a javascript alert. My
problem is the redirect.. ideally i would like the $.ajax object to
recognize the status is 302 and then redirect to the appropriate
location:

This is what i have currently, as one can see i simply see if the
returned "data" string begins with a "/" and then redirects otherwise
do an alert(). Does anyone have a more elegant way of doing this?
Also, not it is probably relevant, but i am using the django
HttpResponse and HttpResponseRedirect objects.

$().ready(function() {
        $("#login_form").submit(function(event) {
                event.preventDefault()
                var data = {}
                data.username = $("input[name='username']", this).val()
                data.password = $("input[name='password']", this).val()

                $.ajax({
                        type: "POST",
                        url: "/user/login/",
                        data: data,
                        success: function(data, status) {
                                s = data.substr(0,1)
                                if (s == '/') {
                                        document.location = data
                                } else {
                                        alert(data)
                                }
                        },
                        error: function(xhr, status, error) {
                                alert(error)
                        }
                })
        })
})

Reply via email to