JQuery ajax request on IOS using Phonegap - Ajax not working

I have a simple html login form deployed in Xcode using the phonegap framework.

When the submitLogin button is clicked, I then submitted the login credentials using an ajax request to validate the login. This is my javascript function to handle login:

function submitLoginForm (login_id, login_pass){
    //here we need to communicate with the php files in the server
    console.log ("debug 1 ");
    $.ajax({
        type: "POST",
        url: "http://192.168.1.9/serverapp/loginHandler.php",
        data: "login_id=" + login_id + "&login_password=" + login_pass,
        success: function(loginStatus){
              navigator.notification.alert ("login request successfully sent");
              console.log ("debug 2");
              if (loginStatus == "success"){
                console.log ("debug 3 ");
                location.href = "main.html";
              }
              else{
                console.log ("debug 4 ");
                location.href="index.html?errcode=1";
              }

        }
    });

   console.log ("debug 5 ");
}

      

After submitting the login form, I see that the console was only printing the message "debug 1". What could be wrong in this case? If the ajax request fails, it should still print the "debug 5" message, but it didn't print anything and just skipped everything except the first debug message.

I could access the php url from my browser, so I'm pretty sure the problem is not in the PHP code. I tried to spend several hours without any results. Do you have a similar problem with the IOS framework for Phonegap and how did you fix it?

+3


source to share


1 answer


The problem has been resolved.

I used an external jquery file from http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

And it turned out that the external jquery url is blocked by the ios simulator, so I have to whitelist that url.



Steps:

  • In your Xcode Cellular Project, navigate to the Resources folder.
  • Go to the File Support directory and edit the PhoneGap.plist file.
  • In the "External Host" list, add all the external urls you want to access (including the jquery external url and any other url you will be using in your ajax request).

I hope this is helpful for other developers who are facing the same problem as mine.

+9


source







All Articles