Search code examples
javascriptiosajaxcordovaphonegap

AJAX post request working in browser, not on Phonegap App


A simple AJAX request is failing when I test it using the Phonegap App on iOS10.

Code is as follows (works perfectly in browser):

if($.trim(firstname).length > 0 & $.trim(email).length > 0 & $.trim(password).length > 0) {

            console.log("input checked");

            $.support.cors = true;
            $.ajax({
                type: "POST",
                url: "http://my-server.com/signup.php",
                data: dataString,
                crossDomain: true,
                cache: false,

                beforeSend: function(data) {
                    console.log("connecting to server");
                },

                //display success/fail message - put something in data on server
                success: function(data, textString, xhr) {
                    if(data == "success") {
                        localStorage.login = "true";
                        localStorage.email = email;
                        mainView.router.loadPage("swipe.html");
                    } else if(data == 'exist') {
                        myApp.alert("Account already exists", "Login Failed");
                    }
                },

                error: function(xhr, ajaxOptions, errorThrown) {
                    console.log("error msg: ");
                    console.log(xhr);
                    console.log(errorThrown);
                    myApp.alert("Unknown error, please try again", "Login Failed");
                },

                complete: function(data) {
                    if(data.readyState == "0") {
                        console.log("unsent");
                    } else if(data.readyState == "4") {
                        console.log("done");
                    }   
                }

            });
        } return false;
    }

I have the config.xml file set up exactly as outlined in many previous answers:

<plugin name="cordova-plugin-whitelist" />
<access origin="http://my-server.com" />

etc...

Obviously disabling apple's App Transport Security is not going to be possible as I am not building the app, simply testing it on a Phonegap server and the Phonegap App.

But is this possibly to do with ATS preventing ajax requests to other domains from the Phonegap App itself? As I suppose it would require Phonegap to allow access to all domains, which probably isn't allowed on the app store!

That is my best guess as to what is happening. Any ideas or workarounds?


Solution

  • the only internet solution for new ios apps is move backend to https...