note 73453 deleted from function.json-encode by felipe
| From: | felipe@php.net | Date: | Mon, 10 Dec 2007 17:30:40 +0000 |
| Subject: | note 73453 deleted from function.json-encode by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133169@lists.php.net to get a copy of this message | ||
Note Submitter: David Gillies (daggillies-AT-gmail.com
----
Although it is perfectly legal to hand back non-XML data as a response to an AJAX request, under
Firefox this will trigger a warning. The simple solution is to wrap the JSON string in a trivial XML
document. I have a simple function called json_message() which does this:
function json_message($message)
{
ob_start();
echo "<?xml version=\"1.0\" standalone=\"yes\" ?>\n";
echo "<json>".rawurlencode( json_encode( $message) )."</json>\n";
$resp=ob_get_clean();
return $resp;
}
The companion function JavaScript side is get_json_message():
function get_json_message(str)
{
var spos=str.indexOf('<json>');
var epos=str.indexOf('</json>');
return unescape(str.substring(spos+6,epos));
}