无法将脚本文件加载到另一个脚本文件中

问题描述:

在已加载的脚本文件中使用$.getJson();时,无法在另一个脚本文件中加载脚本文件.

Not able to load the script file inside another script file when using $.getJson(); inside the loaded script files.

考虑 fisrt.html :

<script>
$.getScript("second.js",function(){
     // some code here
});
</script>

我的第二个js文件是这样的: second.js:

my second js file is like: second.js:

$.getJSON("somefile.json",function(){
});

second.js 中删除$.getJSON时,文件加载完美,没有任何问题.

When remove the $.getJSON from the second.js the files load perfectly without any problem.

如何用$.getJSON加载json文件?

How can I load the my json file with $.getJSON?

complte json调用:

complte json calling:

$.getJSON('js/offline.json', function(data) {
            alert("success");

        }.error(function(data){
            alert(JSON.stringify(data));
        });

您正在尝试同时使用promise和标准回调API.这就是你想要的.

You're trying to use the promise and standard callback API at the same time. Here's what you want.

$.getJSON("js/offline.json")
  .done(function (data) {
    alert(data);
  })
  .fail(function (err) {
    alert(err);
  });