2016-10-24 40 views
0

我试图从URL“https://data.raleighnc.gov/resource/5ccj-g2ps.json”加载数据集,但它需要一个API密钥。我没有与D3或Jquery运气。使用API​​密钥加载D3 Url

我该如何去做这件事,以便我可以加载JSON格式的数据集?

我有以下几点:

$.ajax({ 
url: "https://data.raleighnc.gov/resource/xce4-kemu.json", 
type: "GET", 
data: { 
    "$limit" : 5000, 
    "$$app_token" : "YOURAPPTOKENHERE" 
} 
}).done(data) { 
alert("Retrieved " + data.length + " records from the dataset!"); 
console.log(data); 
}); 

它说我有一个错位“{”,但我看不出。

+1

试图将其加载到我的浏览器中我没有遇到任何问题。你有什么问题? - 你能发布你的AJAX调用代码吗?你在控制台中遇到什么错误? – nicovank

+0

@nicovank更新了它 – Kalimantan

+0

你为什么在d3上使用AJAX?为什么不简单地使用'd3.json'? –

回答

1

有很多在你的代码错误...

$.ajax({ 
    url: "https://data.raleighnc.gov/resource/xce4-kemu.json", // didn't you want to get another URL??!? 
    type: "GET", 
    dataType: "json", 
    data: { 
     "$limit" : 5000, // Does the API require the dollar signs? Looks weird. 
     "$$app_token" : "YOURAPPTOKENHERE" // Did you actually replace with your API key? 
    }, 
    success: (data) => { 
     alert("Retrieved " + data.length + " records from the dataset!"); 
     console.log(data); 
    }, 
    error: (xhr, textStatus, errorThrown) => { 
     // error 
    } 
}); 

应该是工作。

+0

所以它似乎工作。但是我不能在该函数之外调用数据。我如何检索数据以使用它并对其进行过滤? – Kalimantan

+0

您必须使用'success'函数中的数据。这是一个异步调用... – nicovank