我试图从yahoo api获取报价历史记录: Yahoo Historical quotes
我遇到了跨源错误,我尝试通过设置正确的xmlHttpRequest标头来解决它。我跟随其他帖子的推荐但没有成功。
我不想使用Yahoo YQL,因为它限制了对〜2年报价历史的响应。 这是我的代码:
<!DOCTYPE html>
<html>
<body>
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// console.log(this.responseText);
console.log(this.getAllResponseHeaders())
}
};
xhttp.open("GET", "http://chart.finance.yahoo.com/table.csv?&a=0&b=1&c=2011&d=3&e=17&f=2017&s=YHOO", true);
xhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xhttp.setRequestHeader("Content-Type", "text/plain");
xhttp.setRequestHeader("cache-control" , "private");
xhttp.setRequestHeader("Access-Control-Allow-Methods" , "DELETE, HEAD, GET, OPTIONS, POST, PUT");
xhttp.setRequestHeader("Access-Control-Allow-Headers", "Content-Type, Content-Range, Content-Disposition, Content-Description");
xhttp.send();
}
</script>
</body>
</html>