Sending HTML as JSON with RequestBuilder

176 views
Skip to first unread message

Milan Cvejic

unread,
Nov 27, 2012, 12:10:11 AM11/27/12
to google-we...@googlegroups.com
Hi,
I am trying to send HTML string to server using RequestBuilder with JSON but without any success.

Everything works well when in HTML there are no double quotes or other JSON charachters.

So I am trying something like this

RichTextArea area = new RichTextArea();

String htmlToSend = area.getHTML();

JSONObject requestParams = new JSONObject();
requestParams.put("some_key", new JSONString(htmlToSend));

Unfortunately that string is not encoded properly to be safe to send with json

So if value retuned from area.getHTML() contains double quotes like this:

<p><a href="http://www.denic.de/de/domains/recht/index.html" target="_blank">www.denic.de/de/domains/recht/index.html</a></p>

request param looks like

{"some_key":"<p><a href="http://www.denic.de/de/domains/recht/index.html" target="_blank">www.denic.de/de/domains/recht/index.html</a></p>"}

JSONString method toString() on the other hand is calling JsonUtils.escapeValue(value) so that string should be escaped. And If I call escapeValue from JsonUtils by myself
i get string encoded but end up with two double quotes at the beginning and the end of that string..

Any help is welcome,
Thanks,
Milan

Thomas Broyer

unread,
Nov 27, 2012, 4:15:39 AM11/27/12
to google-we...@googlegroups.com


On Tuesday, November 27, 2012 9:18:16 AM UTC+1, Jose Javier García Zornoza wrote:
This way I'm sending html with JSON without problems:


public final class HtmlJSONWrapper extends JavaScriptObject {

   public static native HtmlJSONWrapper create(String jsonStr) /*-{
return JSON.parse(jsonStr);

You can use com.google.gwt.core.client.JsonUtils instead of JSNI here.

But I don't understand why it's not working in Milan's case: it *should* work, there are unit-tests checking that it works: https://code.google.com/p/google-web-toolkit/source/browse/tags/2.5.0/user/test/com/google/gwt/json/client/JSONTest.java#187

Jose Javier García Zornoza

unread,
Nov 27, 2012, 5:24:02 AM11/27/12
to google-we...@googlegroups.com
I think it's not related but with Internet Explorer 8 I'm having a JSON problem with code that runs on other browsers, it says that 'JSON is undefined'

Milan Cvejic

unread,
Nov 27, 2012, 11:29:06 AM11/27/12
to google-we...@googlegroups.com
Thanks guys for help, I forgot to mention that json gets properly
encoded if using

RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
builder.setHeader("Content-Type", "application/json");

but in my case i need request to be sent as
application/x-www-form-urlencoded or as text/html.

Any idea?
Thanks.
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-web-toolkit/-/ZV0hnd2KKSUJ.
>
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-web-tool...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.



--
LiquidBrain!

Thomas Broyer

unread,
Nov 27, 2012, 11:46:30 AM11/27/12
to google-we...@googlegroups.com


On Tuesday, November 27, 2012 5:30:08 PM UTC+1, Milan Cvejic wrote:
Thanks guys for help, I forgot to mention that json gets properly
encoded if using

RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
        builder.setHeader("Content-Type", "application/json");

but in my case i need request to be sent as
application/x-www-form-urlencoded or as text/html.

Ah, so you need an extra level of escaping: URL.encodeURIComponent for applicationx-www-form-urlencoded, or SafeHtmlUtils for text/html (that one really depends how you're processing the data on the server side).

Milan Cvejic

unread,
Nov 27, 2012, 12:05:58 PM11/27/12
to google-we...@googlegroups.com
Thanks Thomas,
I will try that one later today.

Best,
Milan
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-web-toolkit/-/sJwuB_k20DUJ.
Reply all
Reply to author
Forward
0 new messages