我正在尝试使用XMLHttpRequest读取音频流,但是出现错误“XMLHttpRequest无法加载。请求的资源上没有'Access-Control-Allow-Origin'头。因此不允许访问来自'null'的内容”。我尝试使用这个示例中的CORS:
<!DOCTYPE html></code>
<code><html></code>
<code><head></code>
<code><meta charset="utf-8"></code>
<code><title>AUDIO</title></code>
<code></head></code>
<code><body></code>
<code><script type="text/javascript"></code>
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;
}</pre><p></p>
<pre><code> // Helper method to parse the title tag from the response.
function getTitle(text) {
return text.match('<title>(.*)?</title>')[1];
}
// Make the actual CORS request.
function makeCorsRequest() {
// All HTML5 Rocks properties support CORS.
var url = 'http://streaming.radionomy.com/VWClassicRock';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
var title = getTitle(text);
alert('Response from CORS request to ' + url + ': ' + title);
};
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
xhr.send();
}
makeCorsRequest();
</script>
</body>
</html>
. If I put url into audio html tag like this
<!DOCTYPE html></code>
<code><html></code>
<code><head></code>
<code><meta charset="utf-8"></code>
<code><title>AUDIO</title></code>
<code></head></code>
<code><body></code>
<code><audio src='http://streaming.radionomy.com/VWClassicRock' controls></audio></code>
<code></body></code>
<code></html>
, then it works. What should i do, to use it with XMLHttpRequest?
http://localhost/whatever
而不是filesystem://path/to/whatever
访问你的页面,对吗?这两个页面是否在同一个域名下? - elziAccess-Control-Allow-Origin
。 - jgillich