Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX works on mobile safari, but not a UIWebView?

I have a basic jQuery ajax function to log the user in via a UIWebView. However, for some reason it returns blank when it's in a UIWebView. It works fine in mobile safari, and chrome and firefox on my computer.

Here's my code:

$("#login_button").live('click',function() {
        var serializeme = $("#login_form").serialize();
        alert(serializeme);
        $.ajax({
            type: "POST",
            url: "http://domain/location/process_login.php",
            data: serializeme,
             success: function(theRetrievedData) {
                var thePlace = theRetrievedData.indexOf("?!?success?!?");
                if (thePlace != -1) {
                    var theArray = theRetrievedData.split("?!?success?!?");
                    var theUrl = theArray[1];
                    $('#content').fadeOut(500);
                    setTimeout( function() {window.location = theUrl;}, 500 );
                } else {
                    alert(theRetrievedData);
                    alert("no bueno");
                }
             }
        });
    });

The theRetrievedData just returns blank on the alert.

Please help!

PS: The app is called "Dudles" in the app store (it's free) if you want to try to login. You will get a blank message from the alert.

like image 507
iosfreak Avatar asked Dec 11 '11 14:12

iosfreak


1 Answers

Can you post your PHP code as well?

I refactored the code you wrote into how I would write it just to see if I could spot any bugs, and nothing seemed glaringly incorrect. Here is what I have so far:

$(document.body).on('click', '#login_button', function () {
    $.ajax({
        type: "POST",
        url: "http://domain/location/process_login.php",
        data: $(this).closest('form').serialize(),
        success: function (response) {
            var delimiter = "?!?success?!?";
            var isSuccessful = response.indexOf(delimiter) !== -1;

            if (!isSuccessful) {
                // handle error
                return;
            }

            $('#content').fadeOut(500, function () {
                location = response.split(delimiter)[1];
            });
        }
    });
});
like image 172
Eli Avatar answered Sep 17 '22 16:09

Eli