【发布时间】:2014-03-13 17:51:46
【问题描述】:
通过 javascript (jQuery) 我正在尝试将某些页面的内容从 Google 站点加载到不同站点的页面中的元素上。使用 jQuery 的负载我得到了预期:
XMLHttpRequest cannot load https://sites.google.com/site/foo/home. No 'Access-Control-
Allow-Origin' header is present on the requested resource. Origin 'http://bar' is
therefore not allowed access.
我尝试使用 CORS,使用下面的代码(我在 http://www.html5rocks.com/en/tutorials/cors/ 找到),但得到了相同的结果。
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() {
var url = 'https://sites.google.com/site/edumonkihelp/test0';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
xhr.onload = function() {return xhr.responseText;};
xhr.onerror = function() {alert('Woops, there was an error making the request.');};
xhr.send();
}
所以我想我有两个问题: 1. 您可以访问 Google 协作平台中页面的内容吗? 2. 还有其他类似的服务可以让您这样做吗?
谢谢!
【问题讨论】:
标签: javascript jquery cors