无法将数据从外部JSON文件读取到我的HTML页面

时间:2018-08-27 18:05:32

标签: javascript json

我想从此JSON文件读取数据: Link

这是我的代码:

<ul id="list"></ul>

Javascript:

https://paste.ubuntu.com/p/X355mznvFw/

错误:

  

未捕获到的SyntaxError:JSON中位于位置0的意外令牌W   JSON.parse

1 个答案:

答案 0 :(得分:0)

该文件不是JSON,而是JSONP。您可以通过执行以下操作来消耗它:

  1. 创建一个JSONP文档将调用并提供数据的全局函数。
  2. 将文档添加为脚本,以便加载并解释为JavaScript。
  3. 对回调中的数据进行任何操作。数据将不是全局的。
// Create a callback function for the JSONP response to call
function WL_HeadlineCallBack(data){
  console.log(data)
}

// Create a script to load the JSONP document
const script = document.createElement("script");
script.src = "THE URL OF THE JSONP DOCUMENT";
document.head.appendChild(script);

// The body of the JSONP document rougly matches:
/*
WL_HeadlineCallBack(
{
    "WebLinkHeadlineData":{
       ...
    }
}
)
*/
相关问题