Server side code with Ajax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
momukhtar
Forum Newbie
Posts: 11
Joined: Thu Mar 06, 2008 8:02 pm

Server side code with Ajax

Post 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)));
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Server side code with Ajax

Post 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!
(#10850)
Post Reply