-1
嘿,夥計們我正在綁定訪問MTA的Web API。我確實有鑰匙,但我無法撥打電話,因爲我不知道如何正確傳遞鑰匙。我在下面的代碼示例中用字符串「This is my key」替換了我的真正密鑰。如何在嘗試訪問API時使用XMLHttpRequest發送密鑰?
代碼
var myRequest = new XMLHttpRequest();
myRequest.open('GET','http://bustime.mta.info/api/siri/vehicle-monitoring.json', 'This is my key');
myRequest.onload = function(){
var data = JSON.parse(myRequest.responseText);
console.log(data[0].comments);
};
myRequest.send();
錯誤
XMLHttpRequest cannot load http://bustime.mta.info/api/siri/vehicle-monitoring.json. Redirect from 'http://bustime.mta.info/api/siri/vehicle-monitoring.json' to 'http://api.prod.obanyc.com/api/siri/vehicle-monitoring.json' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
「我不知道如何正確傳遞密鑰」 - 這取決於API期望密鑰的呈現方式。 – Quentin