我正在尝试从javascript中检索所有gmail联系人的照片图片。
使用https://code.google.com/p/google-api-javascript-client/
当我尝试向图片网址发出授权的AJAX GET请求时,收到以下错误消息:
XMLHttpRequest无法加载。 Access-Control-Allow-Origin不允许使用Origin。
这就是我提出请求的方式:
$.ajax({ url: "https://www.google.com/m8/feeds/photos/media/atameem%40spokeo.com/16e8f6a1892842c7", data: authParams, success: function(data) { console.log(data); } });
我在浏览器网络标签中看到的带有访问令牌的GET URL是:
任何帮助将不胜感激!
感谢。
答案 0 :(得分:0)
当您尝试从除请求之外的其他某个域发出ajax调用时,会显示此错误。您无法进行跨域ajax调用来检索响应。见:https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS。
但是,您可以设置标题以允许跨源资源共享(CORS):http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing
您也可以通过凭据申请任何文件:
$.ajaxSetup({
type: "POST",
data: {},
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true
});
最新主要浏览器支持CORS。您可以通过以下代码段检查您的浏览器是否支持:
//Detect browser support for CORS
if ('withCredentials' in new XMLHttpRequest()) {
/* supports cross-domain requests */
document.write("CORS supported (XHR)");
}
else{
if(typeof XDomainRequest !== "undefined"){
//Use IE-specific "CORS" code with XDR
document.write("CORS supported (XDR)");
}else{
//Time to retreat with a fallback or polyfill
document.write("No CORS Support!");
}
}