mcgrandlej
mcgrandlej

Reputation: 35

jQuery AJAX call returns entire PHP file

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.

Upvotes: 1

Views: 3485

Answers (2)

xlnt.josef
xlnt.josef

Reputation: 1

In my case, the same problem occurred, once I upgraded php7.4 to php8.1:

enabling PHP in apache2; solved my problem.

a2enmod or a2dismod to enable/disable modules by name.

From the terminal; execute the following cmds solved my problem sudo a2enmod php8.1 && sudo systemctl restart apache2

You too may try this; Ref: Enable PHP Apache2

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

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.

Upvotes: 2

Related Questions