2016-10-24 37 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