Page 1 of 1

Server side code with Ajax

Posted: Fri Apr 11, 2008 1:07 pm
by momukhtar
Hi

I am developing server side code for Ajax which receives a JSON object from client. But the JSON object which I receive on the server contains contains \ for no reason whatsoever. e.g:
I am receiving
{\"coords\":[{\"id\":\"M0DyTx0\",\"xy\":[20,120]}]}
instead of
{"coords":[{"id":"M0DyTx0","xy":[0,100]}]}

The related code, server side code is :

Code: Select all

fwrite($fp, $_POST['data']);
and client side is

Code: Select all

 
var req = new XMLHttpRequest();
req.open( POST, "http://" + window.location.host + ":82/test.php", true );
req.send('data=' + encodeURIComponent(JSON.stringify(this.changes)));
 

Re: Server side code with Ajax

Posted: Fri Apr 11, 2008 1:32 pm
by Christopher
You probably have magic_quotes_gpc turned on. Turn it off or do something like:

Code: Select all

$data = json_decode(get_magic_quotes_gpc() ? stripslashes($json) : $json);
FYI - I found this answer in the online PHP manual in the comments for json_decode(). You can do that too!