This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

Click the Button.

A script tag with a src attribute is created and placed in the document.

The PHP file returns a call to a function with the JSON object as a parameter.

// clickButton() function to call "demo_jsonp.php" file
function clickButton() {
  let s = document.createElement("script");
  s.src = "include/PHP/demo_jsonp.php";
  document.body.appendChild(s);
}

// display myObj.name value
function myFunc(myObj) {
  document.getElementById("demo").innerHTML = myObj.name;
}

// source JSONP PHP file and its contents
<s.src = "include/PHP/demo_jsonp.php">
<?php
$myJSON = '{ "name":"John", "age":30, "city":"New York" }';

echo "myFunc(".$myJSON.");";
?>