getJSON issue with complex URLs

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
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?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
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.
 

beginner99

Diamond Member
Jun 2, 2009
5,210
1,580
136
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?

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
 

Dratickon

Junior Member
May 13, 2012
21
0
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]
);
 
Last edited:

Leros

Lifer
Jul 11, 2004
21,867
7
81
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);
 
Last edited: