首页 > 解决方案 > JSON:从 Github 检索 JSON 文件

问题描述

在一段时间不使用 JSON 之后,我对从另一个域/网络服务器请求数据的可能性有点生疏了。

当 JSON 文件从 GitHub 托管时,我只是试图让 JSON 资源加载到我的 Web 服务器上。使用 AnyOrigin,我使用了这个脚本

$( document ).ready(function() {

  $.ajax({
    dataType: "json",
    url: 'http://anyorigin.com/get?url=,https%3A//raw.githubusercontent.com/testuser/testbin/master/data/&callback=?',
    success: function(result){
      run(result);
    }
  });

});

在我的 index.php 中,我有一个引用 json 的文件。但它不解析它。

<a href="https://raw.githubusercontent.com/testuser/testbin/master/data/<?php echo $post['filename']; ?>">

在这一点上,我有点迷失了。我究竟做错了什么?我没有正确指定一些东西吗?感谢所有帮助。感谢您的时间。

标签: phpjsonajax

解决方案


当您向外部链接发送请求时,您需要在 ajax 请求中使用 JSONP 作为数据类型。此外,还需要在回调函数中接收响应为 JSON。

有关更多详细信息,请查看https://www.w3schools.com/js/js_json_jsonp.asp

这个例子可能对你也有帮助https://www.sitepoint.com/jsonp-examples/


推荐阅读