【问题标题】:getting entire json data from API with AJAX使用 AJAX 从 API 获取整个 json 数据
【发布时间】:2020-09-23 18:18:47
【问题描述】:

通过我的 AJAX 调用,我想完全获取 json 数据。

例如 www.abc.com/api/3 给出了 {"information":.... json data with many levels...}}

我想将此数据存储在变量中。所以我试试这个:

$.ajax({
            async: false,
            type: 'GET',
            dataType: 'json',
            url: url,
            data: data,
            success: function(data) {

                               x=data;// It's wrong, but I don't know how to put whole json into x

            }
        })

【问题讨论】:

  • letvar 初始化x 以避免混淆全局命名空间。否则,您的代码应该可以工作。将x 记录到控制台会得到什么?

标签: javascript jquery json ajax web


【解决方案1】:

您可以将$.ajax() 用作Promise

$.ajax({
    type: 'GET',
    url: url,
}).then((data) => {
  console.log(data);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    相关资源
    最近更新 更多