英文:
get JSON from another site and turn into array or csv
问题
我正在尝试将跨域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) {
//对JSON中的每一行进行操作
myArray.push(item);
});
}
}
});
如果唯一的方法是在服务器上进行操作,那么如何解析JSON并将键"1"的值转换为Go(Golang)中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
得分: 2
var json = 'http://hawttrends.appspot.com/api/terms/';
$.getJSON(json, function(trends){
$.ajax({
type:'GET',
url:json,
dataType:'JSONP',
data: trends,
success: function(msg){
// 使用msg进行操作
}
});
});
由于trends
返回了$.getJSON
的数据,你稍后运行了AJAX。如果你有服务器访问权限,这样做没有什么理由。
使用PHP:
英文:
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
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论