gpt4 book ai didi

javascript - 从另一个站点获取 JSON 并转换为数组或 csv

转载 作者:数据小太阳 更新时间:2023-10-29 03:07:32 24 4
gpt4 key购买 nike

我正在尝试将这个跨域 JSON 的“1”键处的值转换为我网站上的 js 数组。

我尝试使用 $.getJSON(),但我遇到了跨域来源错误。我尝试了 AJAX 并遇到了跨域来源错误。

有什么办法可以解决这个问题并使用 JSON 吗?

这是我使用 $.getJSON() 的尝试:

var trends = '';
var json = 'http://hawttrends.appspot.com/api/terms/';
$.getJSON(json, function(trends){
console.log(trends["1"]);
});

这是我的 AJAX 尝试:

    $.ajax({
type:'GET',
dataType:'jsonp',
data:{},
url:'http://hawttrends.appspot.com/api/terms/',
error:function(jqXHR, textStatus, errorThrown){
console.log(jqXHR);
},
success:function(msg){
if (msg) {
var myArray = [];
$.each(msg, function(i, item) {
//do whatever you want for each row in json
myArray.push(item);
});
}
}
});

如果这样做的唯一方法是在服务器上。我如何解析 JSON 并将键“1”的值转换为 Go (Golang) 中 CSV 文件中的元素。

最佳答案

喜欢:

var json = 'http://hawttrends.appspot.com/api/terms/';
$.getJSON(json, function(trends){
$.ajax({
type:'GET',
url:json,
dataType:'JSONP',
data: trends,
success: function(msg){
// do stuff with the msg
}
});
});

由于 trends$.getJSON 返回数据,您可以稍后运行 AJAX。如果您有服务器访问权限,那么确实没有理由这样做。

使用 PHP:

<?php
$dataArray = json_decode(file_get_contents('http://hawttrends.appspot.com/api/terms/'));
// $dataArray has all data
?>

关于javascript - 从另一个站点获取 JSON 并转换为数组或 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24026056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com