我有一个问题: 我在Popup中尝试使用Google / Gmail授权。
这是我的JavaScript代码:
var OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?';
var SCOPE = 'https://www.google.com/m8/feeds/';
var VALIDURL = 'https://www.localhost:1987/Home/GetContacts?code=';
var CLIENTID = 'MyClientId';
var REDIRECT = 'http://localhost:1987/Home/GetContacts'
var TYPE = 'code';
var _url = OAUTHURL + 'scope=' + SCOPE + '&client_id=' + CLIENTID + '&redirect_uri=' + REDIRECT + '&response_type=' + TYPE;
function login() {
var win = window.open(_url, "windowname1", 'width=800, height=600');
var pollTimer = window.setInterval(function () {
try {
console.log(win.document.URL);
if (win.document.URL.indexOf(REDIRECT) != -1) {
console.log("NOW THIS WINDOW WLL BE CLOSE!");
window.clearInterval(pollTimer);
var url = win.document.URL;
acToken = gup(url, 'code');
win.close();
validateToken(acToken);
}
} catch (e) {
console.log(e);
}
}, 100);
}
function validateToken(token) {
console.log("Text before my ajax");
$.ajax({
url: '/Home/GetContacts?code=' + token,
dataType: "json",
type: "GET",
contentType: 'application/json; charset=utf-8',
data: {},
success: function (data) {
console.log(data);
var info = $.parseJSON(data);
$('#ContactsList').empty();
for (var i = 0, len = info.length; i < len; i++) {
console.log(info[i].ContactMail.ContactMail);
var contact = $('<div/>');
contact.text(info[i].ContactMail);
contact.text(info[i].ContactName);
$('#ContactsList').append(contact);
}
},
error: function (xhr) {
console.log("Error");
console.log(xhr);
}
})
}
//credits: http://www.netlobo.com/url_query_string_javascript.html
function gup(url, name) {
name = name.replace(/[[]/, "\[").replace(/[]]/, "\]");
var regexS = "[\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(url);
console.log(results);
if (results == null)
return "";
else
return results[1];
}
function Test()
{
var win = window.open("http://www.google.com", "windowname1", 'width=800, height=600');
win.close();
}
这是我的简单页面:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>GoogleContacts</title>
<script src="~/Scripts/jquery-1.11.1.js"></script>
<script src="~/Scripts/jquery-ui.js"></script>
<script src="~/Scripts/ImportContacts.js"></script>
</head>
<body>
<div>
<a href='#' onclick='login();'> click here to login </a><br/>
<a href='#' onclick='Test();'> IE Test</a>
<div id="ContactsList">
</div>
</div>
</body>
</html>
在2个浏览器(Chrome,Firefox)中,它运行良好。 但是在Internet Exploler中,我得到了下一个错误: TypeError:无法获取未定义或空引用的属性“文档”
在两个链接中(仅用于快速检查)