简体   繁体   中英

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.

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];
            });
        }
    });
});

try to send ajax request with async:true, like this:

$("#login_button").live('click',function() {
    var serializeme = $("#login_form").serialize();
    alert(serializeme);
    $.ajax({
        type: "POST",
        async:true,
        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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM