I’m working on a simple web application that utilizes JavaScript for the frontend and PHP for the backend. Unfortunately, whenever I attempt to make an AJAX request to my PHP file, it ends up returning the raw source code of the PHP file itself instead of executing it and sending me the expected output.
Here’s the JavaScript code I have:
$(document).ready(function() {
$.get({
url: "api.php",
dataType: "json",
success: function(response) {
console.log(response);
},
error: function() {
console.log("Request failed");
}
});
});
And here is my PHP file (api.php):
<?php
header('Content-Type: application/json');
$message = "Hello World";
echo json_encode($message);
?>
I’m currently running a local PHP server with the command php -S localhost:8000 and I’m using the Live Server extension for the HTML file. Both of these files are located in the same directory. Instead of receiving “Hello World” as JSON, the AJAX request is returning the complete PHP code as plain text. Could you please help me figure out what might be wrong with either my server configuration or the structure of my code?