1

I've been stumped on this for a few days and have looked at a number of different posts without much luck.

I make an AJAX call using jQuery like so:

console.log("Sending post request");
  var postData = {
    "function": "connectToGame",
    "name" : name,
  };
  console.log(postData);
  $.ajax({
    type: "POST",
    url: 'http://localhost:8000/Cordova/server/database.php',
    data: postData,
    dataType: 'text',
    success: function(data)
    {
      console.log("AJAX Success! See return:");
      console.log(data);
      var id = data[0];              //get id

      console.log('Server returned: ' + id + ' as the id');
    },
    error: function(e){
      console.log("AJAX Failure!");
      console.log(e);
    }
  });

After removing a lot of code trying to narrow down the problem, my server/database.php currently looks like this:

<?php
echo "1";
?>

After making the AJAX call the console shows this:

database.js:21 Sending post request
database.js:26 Object {function: "connectToGame", name: ""}
database.js:34 AJAX Success! See return:
database.js:35 <?php
echo "1";
exit;
?>

database.js:38 Server returned: < as the id

I am completely stumped as to why the AJAX call is returning the entire PHP file and any help would be greatly appreciated.

Anders
  • 7,818
  • 9
  • 49
  • 80
mcgrandlej
  • 35
  • 1
  • 5

1 Answers1

2

It looks like you haven't properly configured your webserver to interpret PHP files and it returns their literal contents without any processing. Unfortunately you haven't provided any details about your server side setup so it's pretty hard to help. But if you are using Apache you may take a look at mod_php so that your PHP script are evaluated by the server and the result is returned to the client. So make sure that you have a working server side script at http://localhost:8000/Cordova/server/database.php before trying to make any client side calls to it.

Community
  • 1
  • 1
Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
  • Yup, in all this I was using 'python -m SimpleHTTPServer'. Calling the database.php file through Apache fixed the problem. Thanks! – mcgrandlej Nov 07 '15 at 21:53