18 Nov 2004 lgs   » (Master)

XML-RPC From Javascript

I've been trying some fresh new concepts lately and it's time to share it with everybody who read this :-)

Sometimes I wish I could reload some part of a website without reloading the whole page. People usually do this by inserting an IFrame and reloading its contents with a Javascript timer or something similar. That's not a bad solution but I was looking for something more flexible.

So I jump into XmlHttpRequest. This is something Microsoft first created for Internet Explorer and then the other browser decided to provide something similar. So, except for the creation code, everything else should be crossbrowser.

So what you can do with XmlHttpRequest? Basically you can open http connections from Javascript so you can assign this kind of functions to onClick events or whatever you want, get some data from a server, and then use that data to update your favourite div. Pretty neat, right?

Well, not everything is so nice. Security issues arise when you want to make PUT request instead of GET ones. Mozilla won't let you make a PUT to a domain different from the domain your page is living on. Please note that http://localhost and http://localhost:8000 are different domains from Mozilla point of view. If you use a url of the form file:/// you just need to give your page some security permissions, but to be honest, file:/// urls are not very useful.

Next point, XML-RPC is cooler than simple HTTP, don't you think so? I was pretty happy to find that Mozilla has a component called nsXmlRpcClient.js (in /usr/lib/[mozilla|firefox]/components) that seemed to do what I wanted. Well, I spent the whole tuesday afternoon trying to make it work with the examples and couldn't make it.

So next day (yesterday) I decided to write my own XML-RPC layer on top of xmlhttprequest and I was pretty succesfull with very simple calls (no arguments). I was very excited to get something useful actually running. Today I just added a lot of sugar to it: multiple arguments calls, autoscrolling debugging console, more descriptive error messages, ...

The last problem was the security one? How do I have an http server (for hosting the pages) and an xml-rpc one at the same port so I can make Mozilla happy with security issues? Well, there are several solutions (Zope, your custom server, ...) but I just write a rewrite rule for Apache so everything that looks like http://localhost/xmlrpc/ is forwarded to my xml-rpc python server which is listening at port 8008. Simple and effective!

For those of you who want something visible I have some demos at http://sexmachine.homelinux.net/test_xmlhttprequest.html . Internet Explorer won't work. muHAHAHAHA

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!