简体   繁体   English

为什么这个函数在ajax调用后执行时停止工作?

[英]Why does this function stop working when executed after an ajax call?

I have the following jsfiddle我有以下jsfiddle

<style>
  .line {
    fill: none;
    stroke: steelblue;
    stroke-width: 2px;
  }
</style>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
  var data = [{ "date": "1-May-10", "close": 2 }, { "date": "1-May-12", "close": 7 }];
  var result = { // a mock version of our response
    "javascript": ["https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.js", "https://cdn.jsdelivr.net/gh/nottoobadbutgoodname/tester/chart.js"
    ]
  }

  function loadAll(resultt) { // throwaway var resultt as we are using our mock result
    console.log("loading....");
    getMultiScripts = function (arr) {
      var _arr = $.map(arr, function (scr) {
        return $.getScript(scr);
      });
      _arr.push($.Deferred(function (deferred) {
        $(deferred.resolve);
      }));
      return $.when.apply($, _arr);
    }
    getMultiScripts(result.javascript.slice(0, -1)).done(function () {
      $("<script/>", {
        src: result.javascript[result.javascript.length - 1]
      }).appendTo("body");
    });
  }

  function main() {
    $(document).ready(function () {
      $.ajax({
        url: "http://127.0.0.1:8000",
        success: loadAll
      });
    });
  }

  main(); // doesn't work
  //loadAll(result); // works!
</script>

running loadAll(result) directly works but when I try it with an ajax call it fails silently.运行 loadAll(result) 直接有效,但是当我使用 ajax 调用尝试它时,它会静默失败。 Essentially, the ajax call grabs some .js files that are then loaded.本质上,ajax 调用会获取一些然后加载的 .js 文件。 Running main will fetch the scripts but doesn't seem to execute them (status code for both is 200 but the status code for chart.js is greyed out when running main() but black when running loadAll()运行 main 将获取脚本,但似乎不会执行它们(两者的状态代码都是 200,但在运行 main() 时,chart.js 的状态代码为灰色,但在运行 loadAll() 时为黑色

EDIT:编辑:

This is weird.这很奇怪。 Since in the example I am just mocking the result I thought I'd try an experiment:由于在示例中我只是在嘲笑结果,我想我会尝试一个实验:

https://jsfiddle.net/rwzm4t75/4/ https://jsfiddle.net/rwzm4t75/4/

Basically, I turn it into a JSONP call and grab some random JSONP data and turn loadAll to the jsonCallback function.基本上,我将其转换为 JSONP 调用并获取一些随机 JSONP 数据并将 loadAll 转换为 jsonCallback 函数。 The chart is now loading consistently but, unfortunately, for the "real world" the result cannot be mocked and the data comes as JSON, not JSONP.图表现在一直在加载,但不幸的是,对于“现实世界”,结果无法模拟,数据以 JSON 形式出现,而不是 JSONP。

To be clear, I've tried a JSON response in my real app and JSONP.需要明确的是,我已经在我的真实应用程序和 JSONP 中尝试了 JSON 响应。 The JSON version will sometimes load but not consistently. JSON 版本有时会加载,但不会一致。 The JSONP version always loads. JSONP 版本始终加载。

Try to execute main() inside document ready:尝试在文档内执行 main() 准备好:

$(document).ready(function () {
    main();
});

function main() {
    $.ajax({
      url: "http://127.0.0.1:8000",
      dataType: 'JSON',
    })
    .done(function(result)){
       loadAll(result);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM