通过JSONP通过CDN的静态JSON文件 [英] Static JSON files over CDN via JSONP

查看:698
本文介绍了通过JSONP通过CDN的静态JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的JSON格式的静态/很少更改的数据.为了提高我的ASP.NET MVC应用程序性能,我想将它们移至CDN(Amazon Cloud Front).

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).

但是,当我这样做时,跨域策略就会启动,jQuery会进行HTTP OPTIONS方法调用而不是HTTP GET调用,Amazon会以"403 Forbidden"响应来拒绝请求.

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可能是解决此问题的一种方法,但是由于文件是静态的,并且在CDN上,因此无法将JSON包装在自定义函数中.但是,我可以使用已知的函数名称重新创建它们.例如:

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 ....}}

我可以做类似的事情:

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

所有文件的"JsonWrapping"函数名称将相同.

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

如果jQuery包裹在与上述相同的函数名称中,那么jQuery是否可以通过JSONP下载JSON数据?我对jQuery JSONP的阅读是,jQuery为JSONP请求创建了一些自定义的一次性使用函数名称.可以覆盖吗?

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?

感谢您的帮助.

推荐答案

我刚刚发现这是有可能的.我是这样解决的:

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);
}

这不理想,因为您与触发Ajax请求的事件失去了绑定.因此,需要进行一些黑客攻击.但是,它可以完成工作. 我非常乐意寻求更好的解决方案.

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.

这篇关于通过JSONP通过CDN的静态JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆