Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-11-2013, 11:19 AM
PM User |
#1
New Coder
Join Date: Jan 2010
Location: Germany
Posts: 52
Thanks: 1
Thanked 2 Times in 2 Posts
Confused ; '+' character missing when sending JSON data to a PHP backend
HI
I need some help with sending JSON data to a PHP backend.
In my client JavaScript I do the below before sending it to the backend.
jsonPost={
'table':cfg.db_table_name ,
'idfield':idfield,
'inputValue':inv,
'task':task ,
'id':id,
'name':name,
'attachTable':cfg.attachTable,
'commentTable':cfg.commentTable
};
cache.push('jsonPost='+(JSON.stringify(jsonPost)));
cfg.backend = cfg.fields.griddir + '/php/' + cfg.backendScripts[task];
HGSGRID.sendCacheEntry();
What surprises me is that, if the inputValue set via the value inv has
the value of "PKI+" the '+' character never makes it to the PHP side.
I tryed it with multiple '+++' in different patterns but none shows up on the PHP side.
Below is what the debugger (Chrome) tells me what jsonPost looks like , after JSON.stringify,
jsonPost: Object
attachTable: "attachements"
commentTable: "cell_comments"
id: "11"
idfield: "id"
inputValue: "PKI<>+"
name: "url"
table: "info"
task: "update"
Here it is displayed form the header (Chrome) what has been send via the http request:
This has been send;
Form Data view URL encoded
jsonPost:{"table":"info","idfield":"id","inputValue":"PKI<>","task":"update","id":"11",
"name:"url","attachTable":"attachements","commentTable":"cell_comments"}
Here is when switching to decode, the + show up but is not sent
Form Data view URL decoded
jsonPost:{"table":"info","idfield":"id","inputValue":"PKI<>+","task":"update","id":"11",
"name":"url","attachTable":"attachements","commentTable":"cell_comments"}
For the moment I can't see what I am doing wrong
Regards
Heinz
01-11-2013, 12:51 PM
PM User |
#2
New Coder
Join Date: Jan 2010
Location: Germany
Posts: 52
Thanks: 1
Thanked 2 Times in 2 Posts
Hi
I think i found the cause;
i used
xmlHttp.open("POST", cfg.backend, false);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded ");
This usually replaces blanks with '+' , however I still don't know why an embedded '+'
is not transfered.
Howerver , I switched to using
xmlHttp.open("POST", cfg.backend, false);
xmlHttp.setRequestHeader("Content-Type", "application/json ");
Made some minor changes on the client side and some minor changes in the backend
like this;
$json=@file_get_contents('php://input');
$_POST=json_decode($json,true);
It' working now.
Regards
Heinz
01-11-2013, 02:53 PM
PM User |
#3
Senior Coder
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
In order to correctly encode all (not only +) characters into URI compliant representations for parameters to be sent via Ajax, you need to use encodeURIComponent() on each parameter's name and value
01-12-2013, 09:38 AM
PM User |
#4
New Coder
Join Date: Jan 2013
Location: Sunnyvale, CA
Posts: 40
Thanks: 3
Thanked 1 Time in 1 Post
+ characters are transformed into blank spaces when they upload to the server. For a quick and painless fix I recommend simply substituting it with another character.
Another example of your problem is with base64 encoding for URLs, see it referenced here:
http://stackoverflow.com/questions/1...ied-base64-url
01-12-2013, 09:48 AM
PM User |
#5
Senior Coder
Join Date: Jun 2007
Location: Urbana
Posts: 3,461
Thanks: 9
Thanked 466 Times in 450 Posts
as a binary transfer, like "application/json", the "+" needs no special handling, and the JSON layer makes sure any non-ascii chars are dumbed down.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8%
IE10:10%
01-12-2013, 11:55 AM
PM User |
#6
New Coder
Join Date: Jan 2010
Location: Germany
Posts: 52
Thanks: 1
Thanked 2 Times in 2 Posts
Thank you for all your help !
As
base64 encoding was mentioned , that is exactly where I stumbled
about the problem with the '
+ ' character, as this character is part of the encoding.
I use base64 encoding for all user input before sending it to the backend.
Switching to use "
application/json " solved the problem for me .
I also use now base64 encoding and JSON to transfer 'unknown' data from the backend
server to my client.
Before that I used XML to do this and had all sorts of trouble.
JSON rules
Regards
Heinz
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 12:52 AM .
Advertisement
Log in to turn off these ads.