将JSON数据作为外部脚本而不是AJAX加载 [英] Loading JSON data as external script instead of AJAX

查看:113
本文介绍了将JSON数据作为外部脚本而不是AJAX加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已向我展示了一个技巧-而不是将数据存储在datafile.json中并用ajax加载,而是将数据封装在datafile.js中的单个对象中,例如var Data = {//所有数据都在这里}.然后将datafile.js作为外部脚本加载到html头

I have been shown a trick - instead of having the data in datafile.json and load it with ajax, the data is encapsulated in a single object in a datafile.js such as var Data = { //all data goes here }. Then the datafile.js is just loaded as external script in the html head

它就像在对象中加载了ajax一样运作良好,这有什么缺点吗?

It works well just as if the objects were loaded with ajax, are there any drawbacks to this?

推荐答案

JSON JavaScript. (在过去,您的想法是您可以简单地eval它...)因此,您所说的文件只是...一个JavaScript赋值语句,存储在一个文件."

JSON is JavaScript. (Back in the day, the idea was that you could simply eval it ...) Therefore, the file that you speak of is simply ... "a JavaScript assignment-statement, stored in a file."

将其存储为单独文件的唯一潜在问题可能是定时漏洞".源代码必须单独检索.我不确定浏览器是否会等待 来执行此操作,因此其他JavaScript代码可能会执行而未看到var Data,这是因为尚未检索到该代码块并且已执行

The only potential issue with storing this as a separate file might be "a timing hole." The source-code must be separately retrieved. I'm not sure if the browser would wait to do that, so it might be possible for other JavaScript code to execute that does not see var Data because that block of code hasn't been retrieved and executed yet.

当您有大量不变的固定数据"时,我通常将包括数据在内的所有内容放入 one (是的,它很大……")JS文件中,这样我就知道它是全部尝试"之前执行.是的,就像您在此处有效地进行操作一样,将 fixed 数据直接直接包含到源文件中具有明显的优势,但是我不确定我是否会看到优势(而且,我可能会看到一个优势). 几个 JS文件.

When you have "lots of invariant fixed data," I customarily put everything including the data into one ("yeah, it's big ...") JS file, so that I know "it's all there" before any of it tries to be executed. Yes, there are definite advantages to simply including fixed data directly into your source-file, as you're effectively doing here, but I'm not sure I see an advantage (and, I might see a hole ...) in using several JS files.

这篇关于将JSON数据作为外部脚本而不是AJAX加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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