New course! Every coder should learn Generative AI!
Try a free lesson+ 3
What you probably want is to make a PHP script that responds with JSON content.
Let's say you have a script named "your_script.php" with content like this except you'll have to do what the FIXME comment says:
<?php
// FIXME: get whatever information you want into $yourArray variable.
// Assuming it is JSON-serializable, you can do the following. If it isn't serializable, you'll have to get some data that is.
header("Content-Type: application/json"); // tell the browser the content is JSON-formatted.
echo json_encode($yourArray);
?>
Test that script by going to it in a web browser before trying to get JavaScript calling it. Seeing the JSON there first should prevent some confusion.
Then with your JavaScript, use something like
$.getJSON("/your_script.php",
function(response) {
alert("Yay! Got some data! response = ", response);
}).fail(function() { alert("Oh No!!! Something did not work. Is URL correct?
Is server running? Add some extra parameters to this and check dev tools network feature to get more info."); });