【发布时间】:2014-05-15 03:00:06
【问题描述】:
我试图对雅虎的符号进行异步调用,建议JSONP API,所以有跨域问题,我已经阅读了this文档并尝试更改它的url,以下是我使用的代码
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
function makeCorsRequest() {
// All HTML5 Rocks properties support CORS.
// var url = 'http://updates.html5rocks.com';
var url = 'http://autoc.finance.yahoo.com/autoc?query=google&callback=YAHOO.Finance.SymbolSuggest.ssCallback';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
console.log(text);
};
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
xhr.send();
}
但问题仍未解决:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
有人知道为什么吗?另外,我将文档中的代码与常规的 ajax 代码进行了比较,它们几乎相同,CORS 是如何工作的? 谢谢
【问题讨论】: