Marco Cuccagna
Marco Cuccagna

Reputation: 105

PhoneGap authentication with Ajax

I’m developing an Andorid application with PhoneGap and I’m trying to do an autentication. I have a simple form in a HTML file, in a JS file at the event click sould start the Ajax that activate a PHP file in the remote server for the autentication, here is the JS:

$("#login").click(function(){
var username=$("#username").val();
var password=$("#password").val();
var dataString="username="+username+"&password="+password;
   
   $.ajax({
        type: "POST",
        url: "http://@@##.altervista.org/test.php",
        data: dataString,
        beforeSend: function(){ $("#login").html('Connecting...');},
        success: function(responce){
            if(responce=='success')
            {
                localStorage.login="true";
                localStorage.username=username;
                window.location.href = "index.html";
            }
            else
            {
                alert("Login error");
                $("#login").html('Login');
            }
        }
    });
return false;});

The PHP file is a simple echo "success". When I run it, it remains stuck in the function beforeSend.

security-policy:

<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" />

config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.phonegap.helloworld" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
    <name>Hello World</name>
    <content src="index.html" />
    <preference name="DisallowOverscroll" value="true" />
    <preference name="android-minSdkVersion" value="14" />
    <plugin name="cordova-plugin-battery-status" source="npm" spec="~1.1.1" />
    ****some plugin, icon and splash that i need***    
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <engine name="android" spec="~5.1.1" />
</widget>

Upvotes: 1

Views: 130

Answers (2)

Marco Cuccagna
Marco Cuccagna

Reputation: 105

Apparently it's a security problem, if i run it in a remote server it's show an xmlhttprequest problem: "Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource,". If i run it through the phonegap developer tool it works.

Upvotes: 1

piterio
piterio

Reputation: 810

Do you have any error in the console? You can go to the Network section and see the request you are sending. But try with this:

$("#login").click(function(){
var username=$("#username").val();
var password=$("#password").val();
var dataString="username="+username+"&password="+password;

   $.ajax({
        type: "POST",
        url: "http://@@##.altervista.org/test.php",
        data: {username: username, password: password},
        beforeSend: function(){ $("#login").html('Connecting...');},
        success: function(responce){
            if(responce=='success')
            {
                localStorage.login="true";
                localStorage.username=username;
                window.location.href = "index.html";
            }
            else
            {
                alert("Login error");
                $("#login").html('Login');
            }
        }
    });
return false;});

Upvotes: 0

Related Questions