在瀏覽器中鍵入此值會自動下載一個txt文件,該文件是json格式的AAPL股票期權數據文本。致電Google財經期權API網址
http://www.google.com/finance/option_chain?q=AAPL&output=json
我想這個數據拉入一個JavaScript變量直接的,所以我可以在瀏覽器解析結果,並顯示數據。
我想:
$.getJSON('http://www.google.com/finance/option_chain?q=AAPL&output=json', function(json) {
console.log(json);
});
,但我得到了No 'Access-Control-Allow-Origin' header is present on the requested resource.
錯誤,因爲在其他帖子看到。
我與阿賈克斯試圖還有:
$.ajax({
dataType: "json",
url: "http://www.google.com/finance/option_chain?q=AAPL&output=json",
}).done(function (data) {
console.log(data);
});
,但得到了同樣的錯誤。
我也嘗試過使用jsonp(&output=jsonp
)在url中,但得到一個Uncaught SyntaxError: Unexpected token)
錯誤,它在Chrome檢查器中返回();
。
我該如何將這個json數據從Google直接拖入JS?
嘗試使用'https://'而不是'http://' –
我不再收到錯誤,但沒有數據被拉出,控制檯中沒有任何內容被打印。謝謝 – brno792
如果您想獲取數據,請嘗試將成功函數放入Ajax對象中。 Ajax返回後,它將轉到成功函數而不是done函數。 Ajax將首先執行done函數,因爲它是一種異步方法。 –