tags:

views:

120

answers:

1

I have a large amount of static/rarely changing data in JSON format. To improve my ASP.NET MVC application performance, I would like to move them to a CDN (Amazon Cloud Front).

However when I do that, the cross domain policy kicks in and jQuery makes a HTTP OPTIONS method call instead of HTTP GET and Amazon denies the requst with "403 Forbidden" response.

JSONP might be a way around this but since the files are static and on a CDN there is no way to wrap the JSON in a custom function. However I can recreate them wrapped with a known function name. For example:

{"LineDetails":{"LineNo":"3109","DbId":9 ....}}

I can do something like:

JsonWrapping({"LineDetails":{"LineNo":"3109","DbId":9 ....}});

The "JsonWrapping" function name will be the same for all the files.

Is it possible for jQuery to download JSON data via JSONP if it is wrapped in the same function name as shown above? My reading of jQuery JSONP is that jQuery creates some custom one time use function name for the JSONP request. Can this be overridden?

Thanks for your help.

+2  A: 

I just found out this is somehow possible. I solved it like so:

$(document).ready(function(){
    $.getJSON("http://example.com/staticjsonfile.json",function(data){
    //callback function isn't here
    }
});
function JsonWrapping(data){
    //It's really here
    alert(data);
}

This is not ideal, as you loose binding with the event that fired the Ajax request. So some hacking is required. However, it kinda gets the job done. I'd be very open to a better solution.

Bart Vangeneugden
I also found this link [http://www.mail-archive.com/[email protected]/msg73370.html] that shows a technique on how to to that using $.getScript to download the script.
Mike Weerasinghe