urlencode in GWT
I was having some difficulty finding exact details about support for url encoding in javascript/GWT. The most correct method for matching urlencode is to use encodeURIComponent. I can’t find the exact link again, but one source said that support for encodeURIComponent was unreliable across JavaScript implementations. Another reference compared escape, encodeURI and encodeURIComponent and their documentation suggested that it should be quite common by now.
I added a JSNI call to use encodeURIComponent as part of my RPC code. I haven’t had any issue with FF or IE, but my testing has hardly been comprehensive. It seems like if there’s an issue with compatibility, this could also be solved by creating a FormPanel, setting it up to POST, then adding inputs with appropriate names and values.
This could be a good place for something like:
FormPanel fp = new FormPanel();
// setup FormPanel (encoding etc)
DOM.setInnerHTML(fp.getElement(), inputs);
where you’ve also set up inputs to be an HTML representation of your data to be posted to the server. But this is just a guess on my part. I haven’t worked with a FormPanel as yet. I would consider it for simplicity in data encoding, the ability to set the encoding properly and for file uploads. But I don’t need file uploads, I can use encodeURIComponent for encoding, and I happen to like my RPC implementation with HTTPRequest. So it’s good enough for now.
Speaking of RPC — I’ve been distracted by work, but I still plan to write up more info on my RPC technique. I’ve been kicking around ideas on the value of changing to JSON encoding, using something like JSON-RPC, or other related topics, but I’m somewhat undecided. The big thing that keeps me from JSON-RPC is that I like being able to bundle multiple calls in a single server transaction. But I do like the idea of returning a service definition. And I was thinking about adding some status codes and such… Several ideas, but I’ll try to keep it simple and get something together soon.
January 18th, 2008 at 12:59 pm
Have a look to:
com.google.gwt.http.client.URL
:)