bhylu.com

Advertisements

AJAX PHP

A PHP endpoint can return JSON to browser code. The client should depend on the response contract rather than the server file extension.

Where to run this

Use a local browser project for this example. It needs browser permissions, additional files, a server endpoint, or a library as described below. The restricted playground cannot provide those dependencies.

A practical example

JavaScript

const response = await fetch("/api/sessions.php");
if (!response.ok) throw new Error("Could not load sessions");
const sessions = await response.json();
console.log(sessions);

What happens

A local PHP endpoint returning a JSON list can supply the data.