我有一個端點https://api.iextrading.com/1.0/tops?symbols=aapl但是當我嘗試使用.getjson與該網址時,我得到一個404錯誤。在api文檔中,它提到它可能是一個jsonp請求,如果是這樣的話,我怎樣才能讓.getjson讀取這個調用。先謝謝你。如何讓json使用.getjson
我已經嘗試的代碼是...
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
</head>
<body>
<h2>Create Object from JSON String</h2>
<p id="demo"></p>
<script>
$.getJSON('https://api.iextrading.com/1.0/stock/tsla', function(data) {
var obj = JSON.parse(data);
document.getElementById("demo").innerHTML = obj.id;
});
</script>
</body>
</html>
我們信任的CORS。 https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS – Stavm
腳本中的網址與描述的網址不符。 'https://api.iextrading.com/1.0/tops?symbols = aapl' – user2340824
我不認爲你需要'JSON.parse(data);'.. getJSON已經解析過JSON結果 - 如果你使用鏈接在你的問題的文本,沒關係,甚至有CORS標題......不知道你爲什麼在代碼中使用不同的鏈接 - 這是無效的 –