【问题标题】:i want to load the data from openweathermap so i can display it but am getting Uncaught RangeError: Maximum call stack size exceeded我想从 openweathermap 加载数据,这样我就可以显示它,但我得到了 Uncaught RangeError: Maximum call stack size exceeded
【发布时间】:2020-08-20 12:35:47
【问题描述】:

我创建了一个 id 为“temporary”的 div,这样我就可以从 openweathermap.org API 插入数据,但我在控制台遇到错误,我不知道为什么。我是从 API 加载数据的新手。这是完整的html代码jsfiddle code here

这是发生错误的脚本

var data;

$("form").click(function(evt){
  evt.preventDefault();
  loadJson();
});

function loadJson(){
  loadJson("https://samples.openweathermap.org/data/2.5/find?q=London&appid=b7e1c74ddc5f04e3e18d2ce29e5d432c&units=metric", displayData);
};

function displayData(temp){
  data = temp;

  $("#temporary").html(data);
};
</script> ```

【问题讨论】:

  • [1] loadJson 呼叫 [2] loadJson,然后 [2] loadJson 无限期呼叫 [3] loadJson。这是一个无限循环。
  • 我该如何纠正?
  • loadJson 是无限递归的。你认为它应该做什么?

标签: javascript html jquery json


【解决方案1】:

您的函数 loadJson 会在您的堆栈未超出时自行调用。

重写这个函数,让它加载json并且不会无限期地调用自己

【讨论】:

  • 我该怎么做才能正确地从 API 加载数据
  • 你可以通过任何方式加载 json - xhr、fetch、socket 连接等
【解决方案2】:

你应该使用不同的名字

var data;

$("form").click(function(evt){
  evt.preventDefault();
  anotherNameForLoadJson();
});

function anotherNameForLoadJson(){
  loadJson("https://samples.openweathermap.org/data/2.5/find?q=London&appid=b7e1c74ddc5f04e3e18d2ce29e5d432c&units=metric", displayData);
};

function displayData(temp){
  data = temp;

  $("#temporary").html(data);
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    相关资源
    最近更新 更多