Fix and optimize AJAX caching

Fixes the encoding of the response content for cached ajax data. Seeds the
cache with content suitable for the response object, avoiding unnecessary
intermediate object creation.