php - 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']; ?>">
在这一点上,我有点迷失了。我究竟做错了什么?我没有正确指定一些东西吗?感谢所有帮助。感谢您的时间。
解决方案
当您向外部链接发送请求时,您需要在 ajax 请求中使用 JSONP 作为数据类型。此外,还需要在回调函数中接收响应为 JSON。
有关更多详细信息,请查看https://www.w3schools.com/js/js_json_jsonp.asp
这个例子可能对你也有帮助https://www.sitepoint.com/jsonp-examples/
推荐阅读
- r - How do we arrange two sets of data according to a specific criteria, eg date, in R
- apache-spark - 当节点数发生变化时,Spark 会给出不同的结果
- javascript - 将 Spring 模型属性传递给 JS 脚本
- angular - 如何以角度映射嵌套接口
- python-3.x - 如何在keras的cnn模型中的密集层之后可视化图像
- excel - Excel - 根据另一个表或列中的值比较和替换单元格值
- asp.net-core - 如何将列表中的行文本连接到.Net Core wep api中的成功文本消息中
- angularjs - 如何在量角器中选择具有相同 Web 元素的复选框
- sql - 如何在sql中为测试组找到类似的分布式控制组?
- css - Bootstrap 4 输入不会在填充处停止