Search code examples
jquerygetjsonencodeuricomponent

$.getJSON url redirects to 404 when data has a string with "/" in it


I have a jQuery.getJSON call with the url string

'/addRecipientToMessage/id/' + message_id + '/recipient_name/' + recipient_name 

I have a problem when the recipient name contains a "/" in it. I tried to encode the url with encodeURIComponent, but it redirects me to 404 page not found.

How could I bypass this?

Thanks :)


Solution

  • Use encodeURIComponent function only on recipient_name:

    '/addRecipientToMessage/id/' + message_id + '/recipient_name/' + encodeURIComponent(recipient_name) 
    

    Or you can try with escape() but it is deprecated after Javascript 1.5

    '/addRecipientToMessage/id/' + message_id + '/recipient_name/' + escape(recipient_name)