this works on the same server ...
Code:
<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title></title>
</head>
<body>
<script>
req = new XMLHttpRequest;
req.open("GET","json.php",false);
req.send(null);
alert(JSON.parse(req.responseText).a);// alerts "I'm a!"
</script>
</body>
</html>
no need to wrap the json
with a function call ...
json.php ...
PHP Code:
<?php
class Foo
{
public $a = "I'm a!";
public $b = "I'm b!";
public $c;
}
$obj = new Foo();
echo json_encode($obj);
?>
okay here is another way
this will work on remote server ...
Code:
<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title></title>
</head>
<body>
<script>
function callback(resp){alert(resp.a)}// alerts "I'm a!"
</script>
<script src = "json.php"></script>
</body>
</html>
the json.php
PHP Code:
<?php
class Foo
{
public $a = "I'm a!";
public $b = "I'm b!";
public $c;
}
$obj = new Foo();
echo "callback(".json_encode($obj).")";
?>