小编典典

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

go

我正在尝试将此跨域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文件中的元素。


阅读 251

收藏
2020-07-02

共1个答案

小编典典

喜欢:

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
?>
2020-07-02