Javascript 在尝试访问API时,如何使用XMLHttpRequest发送密钥?

Javascript 在尝试访问API时,如何使用XMLHttpRequest发送密钥?,javascript,ajax,web,xmlhttprequest,Javascript,Ajax,Web,Xmlhttprequest,嘿,伙计们,我想访问MTA的Web API。我确实有钥匙,但我不能打电话,因为我不知道如何正确地传递钥匙。在下面的代码示例中,我用字符串“This is my key”替换了我的真实密钥 代码 var myRequest = new XMLHttpRequest(); myRequest.open('GET','http://bustime.mta.info/api/siri/vehicle-monitoring.json', 'This is my key'); myRequ

嘿,伙计们,我想访问MTA的Web API。我确实有钥匙,但我不能打电话,因为我不知道如何正确地传递钥匙。在下面的代码示例中,我用字符串“This is my key”替换了我的真实密钥

代码

 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密钥必须作为名为
key
的GET参数传递,例如

http://bustime.mta.info/api/siri/vehicle-monitoring.json?key=<your key>…
http://bustime.mta.info/api/siri/vehicle-monitoring.json?key=…
但是,您尝试访问的API未启用跨域访问。它不能直接从Javascript web应用程序中使用——键不会产生任何影响。

,API键必须作为一个名为
key
的GET参数传递,例如

http://bustime.mta.info/api/siri/vehicle-monitoring.json?key=<your key>…
http://bustime.mta.info/api/siri/vehicle-monitoring.json?key=…

但是,您尝试访问的API未启用跨域访问。它不能直接从Javascript web应用程序中使用,因为密钥不会产生任何影响。

“我不知道如何正确传递密钥”-这取决于API希望如何呈现密钥。“我不知道如何正确传递密钥”-这取决于API希望如何显示密钥。此API密钥将公开,并且任何人都可以轻松访问!此API密钥将公开,任何人都可以轻松访问!