Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从其他站点获取JSON并转换为数组或csv_Javascript_Jquery_Ajax_Json_Go - Fatal编程技术网

Javascript 从其他站点获取JSON并转换为数组或csv

Javascript 从其他站点获取JSON并转换为数组或csv,javascript,jquery,ajax,json,go,Javascript,Jquery,Ajax,Json,Go,我正在尝试将这个跨域JSON的键“1”处的值转换为我站点上的js数组 我尝试使用$.getJSON(),但遇到了跨域源错误。我尝试了AJAX,但出现了一个跨域源错误 我有没有办法绕过这个问题,使用JSON 下面是我尝试使用$.getJSON(): 以下是我的AJAX尝试: $.ajax({ type:'GET', dataType:'jsonp', data:{}, url:'http://hawttrends.appspot.com/api/terms/

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

我尝试使用
$.getJSON()
,但遇到了跨域源错误。我尝试了AJAX,但出现了一个跨域源错误

我有没有办法绕过这个问题,使用JSON

下面是我尝试使用
$.getJSON()

以下是我的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
?>


trends[“1”]上有一个数组。转到jSON所在的站点,在页面中找到
“1”
。那里有一个数组:
“1”:[“苗条男人”、“蕾哈娜”、“乔治·荣格”、“凡人快棒X”、“童子军威利斯”、“iOS 8”、“黑鹰”、“权力游戏”、“WWE”、“克里斯·布朗”、“劳伦·斯克鲁格斯”、“格温多琳·克里斯蒂”、“AAPL”、“让·卡森”、“马刺”、“安·B·戴维斯”、“地震”、“刘易斯·卡茨”、“路易斯·蒙特斯”、“OTF·努努”]
但是我怎样才能访问js中的php变量呢?您可以使用
$.ajax({})
,将数据发送到服务器,在那里可以从上面的php示例中获得这些信息。您
echo json\u encode($dataArray),该页面将在所述AJAX查询
成功
后返回,并将结果传递给分配给
success
属性的匿名函数。如果使用AJAX发送数据进入数据库,请确保在服务器端运行正则表达式,因为客户端可以更改数据。
<?php
$dataArray = json_decode(file_get_contents('http://hawttrends.appspot.com/api/terms/'));
// $dataArray has all data
?>