Skip to content
Advertisement

Passing Value Including Spaces on Ajax Call

Trying to pass spaces along with ajax call.

‘word’ is been passed the same as ‘word ‘ i believe so.

On the other hand two words need to be send completely with call.

‘word second’ but not the same as ‘word second ‘

Should I trim before call or do this on server side script? How can I send spaces as well?

Advertisement

Answer

I know this is an old question, but I’d like to point out that the accepted answer is suggesting a function that is deprecated as of JavaScript version 1.5.

Instead, you should use either encodeURI() or encodeURIComponent() for sending spaces and other special characters.

var param = encodeURIComponent("word second "); 
console.log(param); // outputs 'word%20second%20'

PHP on the other end will handle the decoding automatically. You should trim server side, as client side code can be edited by users to circumvent trimming, potentially causing bugs or vulnerabilities.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement