I’m making an AJAX call with $.post(url, cb). The URL I’m passing in could potentially have weird characters like spaces, &, ? and so on.
Do I have to use $.post(encodeURIComponent(url), cb)?
url is something like /foo/weird-char§.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You will have to use
encodeURIComponent()but not on the entire URI, only on the data part (weirdandcharsin your example). The URL and the? &separating the parameters must stay intact. If you encode the entire URI, it will become unusable.If you would add the data as POST data using the
dataparameter:jQuery’s Ajax functions would take care of URL encoding the data automatically.