note 95667 added to function.json-encode
| From: | garydavis at gmail dot com | Date: | Thu, 14 Jan 2010 15:38:36 +0000 |
| Subject: | note 95667 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164980@lists.php.net to get a copy of this message | ||
If you are planning on using this function to serve a json file, it's important to note that
the json generated by this function is not ready to be consumed by javascript until you wrap it in
parens and add ";" to the end.
It took me a while to figure this out so I thought I'd save others the aggravation.
<?php
header('Content-Type: text/javascript; charset=utf8');
header('Access-Control-Allow-Origin: http://www.example.com/');
header('Access-Control-Max-Age: 3628800');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
$file='rss.xml';
$arr = simplexml_load_file($file);//this creates an object from the xml file
$json= '('.json_encode($arr).');'; //must wrap in parens and end with
semicolon
print_r($_GET['callback'].$json); //callback is prepended for json-p
?>
----
Server IP: 208.69.120.55
Probable Submitter: 165.170.128.65
----
Manual Page -- http://www.php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/95667
Del: integrated -- https://master.php.net/note/delete/95667/integrated
Del: useless -- https://master.php.net/note/delete/95667/useless
Del: bad code -- https://master.php.net/note/delete/95667/bad+code
Del: spam -- https://master.php.net/note/delete/95667/spam
Del: non-english -- https://master.php.net/note/delete/95667/non-english
Del: in docs -- https://master.php.net/note/delete/95667/in+docs
Del: other reasons-- https://master.php.net/note/delete/95667
Reject -- https://master.php.net/note/reject/95667
Search -- https://master.php.net/manage/user-notes.php