getJSON issue with complex URLs

Discussion in 'Programming' started by TechBoyJK, Oct 31, 2012.

  1. TechBoyJK

    TechBoyJK Lifer

    Joined:
    Oct 17, 2002
    Messages:
    16,543
    Likes Received:
    43
    I'm trying to get an AJAX function working, and it otherwise works except for when one of the included arguments is a string with a bunch of special characters (like a pw).

    Code:
    
    $.getJSON("http://company.com/access.cfc?method=checkUserCredentials&userID="+ #session.myID# + "&lastname=" + lastName + "&firstname=" + firstName+ "&email=" + email + "&password=" + password,function(checkUserCredentialsResults)
    
    
    This works fine if the password doesn't contain any prohibition/special characters.

    For instance.. 'test123' works fine, but 'test!@#456' gets truncated to 'test!@'

    Do I need to encode this url?
     
  2. Loading...

    Similar Threads - getJSON issue complex Forum Date
    html/css/php issue. New page load always goes white first Programming May 14, 2017

  3. Crusty

    Crusty Lifer

    Joined:
    Sep 30, 2001
    Messages:
    12,688
    Likes Received:
    1
    You need to encode every piece of data you put into a URL query string. That's what the definition of a URL is. I'm assuming that this sets up some sort of session on the server so I think you really should be using a POST request here anyways.
     
  4. beginner99

    beginner99 Diamond Member

    Joined:
    Jun 2, 2009
    Messages:
    3,171
    Likes Received:
    94
    I just hope this application is not facing the internet...or its https.

    http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript
     
  5. TechBoyJK

    TechBoyJK Lifer

    Joined:
    Oct 17, 2002
    Messages:
    16,543
    Likes Received:
    43
    Yes, it would be moved to https before production.
     
  6. Dratickon

    Dratickon Junior Member

    Joined:
    May 13, 2012
    Messages:
    21
    Likes Received:
    0
    I'd also recommend using POST rather than GET. Either way, jQuery will handle the encoding if you let it build the request itself.
    Code:
    $.POST(
    'http://company.com/access.cfc',
    {
    method : 'checkUserCredentials',
    userID : '#session.myID#',
    lastname : lastName,
    firstname : firstName,
    email : email,
    password : password,
    },
    [I]your callback,[/I]
    'json'
    );
    
    Using getJSON works just as well, but you just need to change how you're feeding it the variables:

    Code:
    $.getJSON(
    'http://company.com/access.cfc',
    {
    method : 'checkUserCredentials',
    userID : '#session.myID#',
    lastname : lastName,
    firstname : firstName,
    email : email,
    password : password,
    },
    [I]your callback[/I]
    );
    
     
    #5 Dratickon, Oct 31, 2012
    Last edited: Oct 31, 2012
  7. Leros

    Leros Lifer

    Joined:
    Jul 11, 2004
    Messages:
    21,883
    Likes Received:
    6
    Alternatively, if you want to be manual, you can call encodeURIComponent().

    Code:
    var url = "http://company.com/access.cfc?method=checkUserCredentials" + 
        "&userID=" + encodeURIComponent(#session.myID#) +
        "&lastname=" + encodeURIComponent(lastName) + 
        "&firstname=" + encodeURIComponent(firstName) + 
        "&email=" + encodeURIComponent(email) + 
        "&password=" + encodeURIComponent(password);
    $.getJSON(url ,function(checkUserCredentialsResults);
    
    
    
     
    #6 Leros, Nov 3, 2012
    Last edited: Nov 3, 2012