Javascript 访问Google站点上的页面以加载到我的站点
通过javascript(jQuery),我试图将Google站点中某些页面的内容加载到其他站点页面中的元素上。使用jQuery的load,我得到了预期的结果:Javascript 访问Google站点上的页面以加载到我的站点,javascript,jquery,cors,Javascript,Jquery,Cors,通过javascript(jQuery),我试图将Google站点中某些页面的内容加载到其他站点页面中的元素上。使用jQuery的load,我得到了预期的结果: 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
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,使用下面的代码(我在中找到),但得到了相同的结果
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.你能访问谷歌网站的页面内容吗?
2.是否有其他类似的服务允许您这样做
谢谢 根据,您可以使用路径
参数获取位于http://sites.google.com/site/siteName/path/to/the/page
:
GET /feeds/content/domainName/siteName?path=/path/to/the/page
为了使用指定dataType:'jsonp'
例子
该示例演示如何检索位于https://sites.google.com/site/nokiaofficescontacts/hq
:
$.ajax({
url: 'https://sites.google.com/feeds/content/site/nokiaofficescontacts?path=/hq&alt=json',
success: function (data) {
console.log('Page was succesfully retrieved');
console.log(data.feed.entry[0].title); //print Page Title
},
error: function (error) {
console.log(JSON.stringify(error));
},
dataType: 'jsonp'
});
注意:alt=json
用于请求json格式的响应
这很有效。我应该但没有预见到的一个问题是,Google使用了许多css类来格式化页面。这些内容自然不会包含在页面内容中。此外,侧栏、页眉和页脚也不会包括在内,它们不是该页面的一部分,而是网站的一部分。一切都很合乎逻辑!