Code Renaissance is about building great teams and great software. By exploring best practices, team interactions, design, testing and related skills Code Renaissance strives to help you create the team and codebase that you've always wanted.

Intercepting jQuery ajax requests

Recently I needed to evaluate various grid options for a website and to keep things simple I decided to fake/intercept ajax calls to inject the data I needed. I needed something that would let me pass in a wait time and response (in my case 10000 records for a grid) as ajax data and I quickly came up with something like this:

Here is a jsFiddle of the same: Simple ajax fake for mockups

Unfortunately that didn't work for Kendo UI Grid - their code calls jQuery.ajax.extend, which lets them take a little more control of ajax and completely bypasses the above code.

I briefly considered tapping into xmlhttprequest, but a cursory look said that would be a lot of work. Then it occurred to me that jquery ajax uses deferreds under the hood and that I could just hack those instead.

Here is a jsFiddle of the same: jQuery Ajax Intercept

With a little time both examples could be modified to be more generic, but for what I was doing it wasn't really worth it (though I cleaned them up for you a little). Hope this helps.