File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Encode java, decode javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Encode java, decode javascript" Watch "Encode java, decode javascript" New topic
Author

Encode java, decode javascript

Andrei Antonescu
Ranch Hand

Joined: Jul 08, 2010
Posts: 75
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Why does the string need to be encoded? jQuery certainly doesn't require that.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Andrei Antonescu
Ranch Hand

Joined: Jul 08, 2010
Posts: 75
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

You never did answer my question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encode java, decode javascript