Where are you defining this bit of code? I presume its in your java program handling the test execution rather than in the source code of the web page itself.
I've not done much javascript at all, but I presume you'd have to define or trigger the override in the source of the web page itself, where you could quite possibly place it before the document ready event. But I'd assume that Selenium only takes control of the browser after the document ready event, so this seems impossible to do, unless there's some nifty way of triggering your java code from your javascript code, but then you'd have to update the source web page somehow to do that, so you might as well just update the source page with your mocked request itself in raw javascript.