Verbose Logging

software development with some really amazing hair

T + G I F R

Hijack AJAX Requests Like A Terrorist

· · Posted in Programming
Tagged with

AJAX requests are a grand thing. They let you request things from your server without refreshing the page. Now, if you are trying to proxy a page, you can rewrite all the links in the page to point back through your proxy, but AJAX requests are another thing.

Oh wait no they're not!

You can't rewrite them when you proxy the page (by proxy, I mean you request my page with a URL param to another page, and I pull in that page, do some stuff, and serve it to you), but you still want the AJAX to go through your proxy, since otherwise it won't work.

Luckily there's a solution!

No matter what framework you use, jQuery, Prototype, whatever, they all go through the XMLHttpRequest interface. That is unless you are rockin' IE6, in which case they use an ActiveXObject. I don't deal with that, although I'm sure you can do something similar with it.

Anyway.

So you have this XMLHttpRequest thing, and as an example, in the jQuery code they do this:

new window.XMLHttpRequest();

See that new in there? They are creating a new object (for varying definitions of object). But whatever, this means we can use the magic of prototype. There's a bunch of stuff out there, so I won't cover it, but let's get some code.