我正在使用 Google 通讯录 JavaScript API。我正在尝试使用中给出的代码将联系人添加到经过身份验证的用户的 gmail 帐户 http://code.google.com/apis/contacts/docs/1.0/developers_guide_js.html#Interactive_Samples .
我可以登录和注销,但我尝试创建新联系人时我的 Chrome 出现错误。我在 Amazon s3 存储桶中托管了 JavaScript 和 html 文件以及图像。
不安全的 JavaScript 试图从 URL 为 about:blank 的框架访问框架 URL 为 https://s3.amazonaws.com/googlecontacts/google_contacts.html .域、协议(protocol)和端口必须匹配。
并且不会创建联系人。
HTML文件
<!DOCTYPE HTML>
<head> <title> Google contacts </title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="auth.js" > </script>
</head>
<body>
<h1> Google contacts </h1>
<img src="rss_icon.jpg" width="100" height="100" />
<input type="button" value="login" onclick="logMeIn()" />
<input type="button" value="logout" onclick="logMeOut()" />
<input type="button" value="createContact" onclick="createContact()" />
</body>
</html>
javascript文件
google.load( 'gdata', '1.x' );
var contactsService;
function setupContactsService() {
contactsService = new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');
}
function logMeIn() {
var scope = 'https://www.google.com/m8/feeds';
var token = google.accounts.user.login(scope);
}
function logMeOut() {
google.accounts.user.logout();
}
function createContact() {
/*
* Create a contact entry
*/
// Create the contacts service object
var contactsService =
new google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');
// The feed URI that is used to create a contact entry
var feedUri = 'http://www.google.com/m8/feeds/contacts/default/full';
// Create an instance of ContactEntry
var entry = new google.gdata.contacts.ContactEntry();
// Set the name of the contact
entry.setTitle(google.gdata.Text.create('JS-Client: Create Contact'));
// Set the content of the contact
entry.setContent(google.gdata.Text.create('content info here'));
// Create an email instance
var email = new google.gdata.Email();
email.setAddress('JS-Client@domain.com');
email.setPrimary(true);
// Designate this email as the "home" email
email.setRel(google.gdata.Email.REL_HOME);
// Add the email instance
entry.setEmailAddresses([email]);
// The callback method that will be called after a successful insertion from insertEntry()
var callback = function(result) {
PRINT('contact entry created!');
}
// Error handler will be invoked if there is an error from insertEntry()
var handleError = function(error) {
document.getWriter='error';
}
// Submit the request using the contacts service object
contactsService.insertEntry(feedUri, entry, callback,
handleError, google.gdata.contacts.ContactEntry);
}
最佳答案
问题是我从 http 服务器访问 https 服务器,所以协议(protocol)不匹配只是更改了 feedURi http://www.google.com/m8/feeds/contacts/default/full ';到 https://www.google.com/m8/feeds/contacts/default/full ';
关于javascript - 不安全的 JavaScript 试图访问 URL 为 :blank from frame with URL 的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6775259/