Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with jQuery $.get in IE

I have a login form which appears at the top of all of my pages when the user is logged out. My current jQuery/javascript code works in Firefox 3 but not IE 7. The code queries a page which simply returns the string "true" or "false" depending on whether the login was successful or not. Inside my $.ready() function call I have the following...

$('#login_form').submit(function() {

        var email = $('input#login_email').val();
        var pw = $('input#login_password').val()

        $.get('/user/login.php', { login_email: email, login_password: pw }, function(data) {
            alert('get succeeded');
            if(data == 'true') {
                $('#login_error').hide();
                window.location = '/user/home.php';
                alert('true');
            }
            else {
                $('#login_error').show();
                alert('false');
            }

        });

        alert('called');

        return false;
    });

In FF, I am successfully transferred to the intended page. In IE, however, the below alerts "called" and nothing else. When I refresh the page, I can see that I am logged in so the $.get call is clearly going through, but the callback function doesn't seem like its being called (ie. "get succeeded" is not popping up). I also don't appear to be getting any javascript error messages either.

Why isn't this working in IE?

Thanks

EDIT: Since a couple people asked, whenever I enter a correct email/password or an incorrect one, nothing in the callback function happens. If I manually refresh the page after entering a correct one, I am logged in. Otherwise, I am not.

EDIT 2: If I alert out data in the callback function nothing happens in IE (I do not get an alert popup). In FF, it alerts true for valid email/pw combos and false for invalid ones. I am using jQuery 1.3.2.

EDIT 3: Ok, guys, I tried R. Bemrose's thing down there and I'm getting a "parseerror" on the returned data. I'm simply echoing 'true' or 'false' from the other PHP script. I also tried 'yes' and 'no', but that still gave me a parse error. Also, this works in Chrome in addition to FF.

like image 741
Wickethewok Avatar asked Jan 23 '23 15:01

Wickethewok


2 Answers

In your response type use:

header("content-type:application/xml;charset=utf-8");

like image 151
rball Avatar answered Feb 01 '23 09:02

rball


As stupid as this sounds... perhaps IE7 is being anal retentive about the missing semicolon on the var pw line?

Probably not, but the only way I can think of getting more information is to convert it to an $.ajax call in order to add an error hook and see which error type it think is happening. Oh, and to check out the exception object.

$.ajax({
        type: 'GET',
        url: '/user/login.php',
        data: { login_email: email, login_password: pw },
        success: function(data) {
                alert('get succeeded');
                if(data == 'true') {
                        $('#login_error').hide();
                        window.location = '/user/home.php';
                        alert('true');
                }
                else {
                        $('#login_error').show();
                        alert('false');
                }
        },
        error: function(xhr, type, exception) {
                alert("Error: " + type);
        }
});

If the error type is parse, IE may be complaining because the data coming back has extra commas at the end of comma separated arrays/lists.

like image 28
Powerlord Avatar answered Feb 01 '23 09:02

Powerlord