Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    hgs
    hgs is offline
    New Coder
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    80
    Thanks
    3
    Thanked 5 Times in 5 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

  • #2
    hgs
    hgs is offline
    New Coder
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    80
    Thanks
    3
    Thanked 5 Times in 5 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

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 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

  • #4
    New Coder
    Join Date
    Jan 2013
    Location
    Sunnyvale, CA
    Posts
    97
    Thanks
    6
    Thanked 7 Times in 7 Posts
    + 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

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 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 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    hgs
    hgs is offline
    New Coder
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    80
    Thanks
    3
    Thanked 5 Times in 5 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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •