Google 联系人 API 和来源错误
Google Contacts API and Origin error
我使用 Google Contacts API 3.0 版,当我想调用其中一个地址以获取联系人列表中的请求数据时,发生了此错误:
XMLHttpRequest cannot load https://www.google.com/m8/feeds/contacts/XXXX@gmail.com/full. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://YYYY.ir' is therefore not allowed access. The response had HTTP status code 405.
这是我的代码:
function PopupFunc() {
jQuery("input[title*='GetgoogleButt1']")
.off()
.on('click', function (event) {
console.log('clicked');
var lnk = "https://accounts.google.com/o/oauth2/v2/auth?" +
//"scope=email profile&" +
"scope=https://www.googleapis.com/auth/contacts.readonly&" +
//"scope=https://www.googleapis.com/auth/user.phonenumbers.read&" +
"state=/profile&" +
"redirect_uri=http://XXXX/Mypagerdi.html&" +
"response_type=token&" +
"client_id=MUCLIENTID.apps.googleusercontent.com&" +
"output=embed";
var authTab = window.open(lnk, "Auth", "_blank, width=600, height=600");
jQuery(window).off().on("message", function (e) {
if ( e.originalEvent.origin == "http://XXXX/" ) {
console.log("message is : ", e.originalEvent.data)
var hashdic = e.originalEvent.data;
var authrz = hashdic["token_type"] + ' ' + hashdic["access_token"];
console.log(hashdic["expires_in"])
console.log(authrz)
if ( hashdic["#error"] == undefined ) {
jQuery.ajax({
url: "https://www.google.com/m8/feeds/contacts/YYYY@gmail.com/full",
headers: {
'Authorization': authrz,
'Content-Type': 'application/json'
},
method: 'GET',
success: function (data) {
console.log('succes: ' + data);
}
});
}
}
});
});
}
PopupFunc();
我在凭据中添加了授权 JavaScript 来源,但此错误仍然存在!
编辑 :
这是 ajax 正文中缺少的部分 dataType: 'jsonp'
感谢@Mehmet
请在您的 ajax 请求中尝试 crossDomain: true 和 dataType: 'jsonp'。
我使用 Google Contacts API 3.0 版,当我想调用其中一个地址以获取联系人列表中的请求数据时,发生了此错误:
XMLHttpRequest cannot load https://www.google.com/m8/feeds/contacts/XXXX@gmail.com/full. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://YYYY.ir' is therefore not allowed access. The response had HTTP status code 405.
这是我的代码:
function PopupFunc() {
jQuery("input[title*='GetgoogleButt1']")
.off()
.on('click', function (event) {
console.log('clicked');
var lnk = "https://accounts.google.com/o/oauth2/v2/auth?" +
//"scope=email profile&" +
"scope=https://www.googleapis.com/auth/contacts.readonly&" +
//"scope=https://www.googleapis.com/auth/user.phonenumbers.read&" +
"state=/profile&" +
"redirect_uri=http://XXXX/Mypagerdi.html&" +
"response_type=token&" +
"client_id=MUCLIENTID.apps.googleusercontent.com&" +
"output=embed";
var authTab = window.open(lnk, "Auth", "_blank, width=600, height=600");
jQuery(window).off().on("message", function (e) {
if ( e.originalEvent.origin == "http://XXXX/" ) {
console.log("message is : ", e.originalEvent.data)
var hashdic = e.originalEvent.data;
var authrz = hashdic["token_type"] + ' ' + hashdic["access_token"];
console.log(hashdic["expires_in"])
console.log(authrz)
if ( hashdic["#error"] == undefined ) {
jQuery.ajax({
url: "https://www.google.com/m8/feeds/contacts/YYYY@gmail.com/full",
headers: {
'Authorization': authrz,
'Content-Type': 'application/json'
},
method: 'GET',
success: function (data) {
console.log('succes: ' + data);
}
});
}
}
});
});
}
PopupFunc();
我在凭据中添加了授权 JavaScript 来源,但此错误仍然存在!
编辑 :
这是 ajax 正文中缺少的部分 dataType: 'jsonp'
感谢@Mehmet
请在您的 ajax 请求中尝试 crossDomain: true 和 dataType: 'jsonp'。