PDA

View Full Version : encoding spaces in JSON.encode



mchahn
8 Feb 2010, 4:50 PM
I am using Ext.util.JSON.encode to serialize a complex object and send it to PHP with Ext.Ajax.request. When I receive it I see that spaces are encoded as plus (+) symbols. Unfortunately a real plus symbol seems to also be encoded as a plus.

How do I tell a plus from a space in PHP? Is there some encoding option I am missing?

mchahn
8 Feb 2010, 5:21 PM
When I do


var values = Ext.getCmp('detail-tabpanel').getForm().getValues();a textfield (at least one) is doing the conversion of spaces to pluses and leaving pluses as pluses.

How do I fix this?

mitchellsimoens
8 Feb 2010, 5:22 PM
Looks like '+' is an alias to a space and once a request goes to Apache/PHP it get's decoded.

So if you send an argument valued at '+ ' then it will be decoded as ' ' (two spaces). What you should probably do is change '+' to it's Hex value '2B' before you send the request. a space's Hex value is '20' and that's why you may see '%20' in the URI in place of spaces.

I have found a few links for you to help understand this:

http://bugs.php.net/bug.php?id=39078&edit=1
Read the comments, it'll help you really understand it.

http://www.freesoft.org/CIE/RFC/1738/4.htm
Read the "Unsafe" section

http://www.ietf.org/rfc/rfc2396.txt
Read section 2.2

http://www.asciitable.com/
Just a good page to see different character codes

mchahn
8 Feb 2010, 5:32 PM
Since I posted the original question, I have found out the problem has nothing to do with json or URLs. I am getting this space and plus ambiguity straight from the textarea field in the form immediately upon using form.getValues().

mchahn
9 Feb 2010, 10:44 AM
For what it is worth, using getFieldValues() instead of getValues() fixed the problem.

I'm still confused as to how one would tell the difference between a space and a plus when using getValues().