dtlab08822 2014-06-03 22:31
浏览 54
已采纳

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

I'm trying to turn the value at the key of "1" of this cross-domain JSON into a js array on my site.

I tried using $.getJSON(), but I encountered a cross-domain origin error. I tried AJAX and got a cross-domain origin error.

Is there any way I can get around this and user the JSON?

Here is my attempt using $.getJSON():

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

Here is my AJAX attempt:

    $.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);
          });
        }
    }
});

If the only way to do this is on a server. How can I parse the JSON and turn the values of the key "1" to elements in a CSV file in Go ( Golang ).

  • 写回答

1条回答 默认 最新

  • doupin5408 2014-06-03 22:41
    关注

    Like:

    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
        }
      });
    });
    

    Since trends returns the data from $.getJSON you run your AJAX later. There is really no reason to do this, if you have server access.

    Using PHP:

    <?php
    $dataArray = json_decode(file_get_contents('http://hawttrends.appspot.com/api/terms/'));
    // $dataArray has all data
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿