user1433479
user1433479

Reputation: 135

Calling PHP script with Cordova

I'm working on an app that's supposed to contain the same information as an already existing website.

What I wanted to do was create a Cordova app that calls an external PHP script which in turn gets information from the database that the website is using.

Right now I'm working on calling the PHP script but it just doesn't seem to work.

Here is the script I'm trying to call:

<?php
 $a = 1;
 $b = json_encode($a);
 return $b;
?>

Ofcourse this is just to test the connection. The URL for this file is http://localhost:8888/get_posts.php

Here is the code for the app:

$('#page1').bind('pageshow', function () {
    $.get('localhost:8888/get_posts.php', function (data) {
        $(this).find('.homeText').html(data);
    });
});

This fetches the file whenever the page is shown (handy) and then puts the new data into the page. The problem is that the page remains empty at all times, when it should be showing a "1". Can anyone see where it goes wrong?

Error message: XMLHttpRequest cannot load localhost:8888/get_posts.php. Cross origin requests are only supported for HTTP.

UPDATE: The error message dissapeared when adding http:// to the url, but the problem persists.

I've changed the code to:

$('#page1').bind('pageshow', function () {
    $.get('localhost:8888/get_posts.php', function (data) {
        alert(data);
    });
});

and it shows me an empty alert box.

Solution: Had to use echo instead of return for the script to show me a result. http:// was also required so the script is allowed to communicate.

Upvotes: 0

Views: 1159

Answers (1)

Sven Delueg
Sven Delueg

Reputation: 1001

You have to 'echo' your response not returning it like so

<?php
 $a = 1;
 $b = json_encode($a);
 echo $b;
?>

Upvotes: 1

Related Questions