• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Encode java, decode javascript

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I want to send a json object from a servlet to a javascript page. The json object contains a field named "text" that must be encoded or it will not be handled properly by the javascript code (Jquery library - $.ajax method with dataType: 'json' enabled).
So I encoded the string inside the servlet with: URLEncode("string body ". "UTF-8") and try to decode it's content in Javascript using: unescape("string body").
It all went fine except for the fact that the sign + was not replaced by space after decoding.

Is there any function in Java that can encode a string so that Javascript's "unescape" function can properly decode it?
I don't wat to use replace.

Thanks in advance
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the string need to be encoded? jQuery certainly doesn't require that.
 
Andrei Antonescu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed the problem in a ugly way. I used URLEncoder.encode in Java and special function in Javascript. I had this problem many times. Not necessary with Jquery indeed.

For example:
I am trying to display a link in a Jquery Datatables plugin. The link is smth like:



Or:
The server returns a JSON string that contains special chars (comma etc.). Jquery $.ajax(... dataType: 'json' doesn't work.
 
Bear Bibeault
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never did answer my question.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic