Javascript 未能执行';后消息';在';DOMWindow';:提供的目标源(';文件://';)与收件人窗口不匹配;s原点(';null';)
我正在访问GoogleAPI来运行一个找到的示例。我已经将为浏览器应用程序生成的API密钥放在Google API控制台中,我无法获得对urlshortner API的简单请求。该API已在同一控制台中启用。我使用了Chrome的开发工具,它给了我以下错误: 未能对“DOMWindow”执行“postMessage”:提供的目标源('file://')与收件人窗口的源('null')不匹配 我做了一些研究,它可能是由Javascript源代码集引起的。但这在凭证的OAuth部分 这是我的代码,与示例完全相同。我把我的钥匙换成了*** 我没有在服务器上运行此Javascript 未能执行';后消息';在';DOMWindow';:提供的目标源(';文件://';)与收件人窗口不匹配;s原点(';null';),javascript,html,google-api,api-key,Javascript,Html,Google Api,Api Key,我正在访问GoogleAPI来运行一个找到的示例。我已经将为浏览器应用程序生成的API密钥放在Google API控制台中,我无法获得对urlshortner API的简单请求。该API已在同一控制台中启用。我使用了Chrome的开发工具,它给了我以下错误: 未能对“DOMWindow”执行“postMessage”:提供的目标源('file://')与收件人窗口的源('null')不匹配 我做了一些研究,它可能是由Javascript源代码集引起的。但这在凭证的OAuth部分 这是我的代码,与
.html
文件。它保存在我的机器上,并在浏览器中运行
我的代码:
<html>
<head>
<script>
function appendResults(text) {
var results = document.getElementById('results');
results.appendChild(document.createElement('P'));
results.appendChild(document.createTextNode(text));
}
function makeRequest() {
var request = gapi.client.urlshortener.url.get({
'shortUrl': 'http://goo.gl/fbsS'
});
request.then(function(response) {
appendResults(response.result.longUrl);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
}
function init() {
gapi.client.setApiKey('***');
gapi.client.load('urlshortener', 'v1').then(makeRequest);
}
</script>
<script src="https://apis.google.com/js/client.js?onload=init"></script>
</head>
<body>
<div id="results"></div>
</body>
</html>
函数结果(文本){
var results=document.getElementById('results');
结果.appendChild(document.createElement('P'));
appendChild(document.createTextNode(text));
}
函数makeRequest(){
var request=gapi.client.urlshortener.url.get({
'短URL':'http://goo.gl/fbsS'
});
请求。然后(函数(响应){
appendResults(response.result.longUrl);
},功能(原因){
console.log('错误:'+原因.结果.错误.消息);
});
}
函数init(){
gapi.client.setApiKey('***');
load('urlshortener','v1')。然后(makeRequest);
}
只是猜测,但您可能需要从服务器上运行该文件。file://
URL不允许以与实际服务器相同的方式进行跨源填充存在一些问题。但是开发人员的控制台不应该为浏览器应用程序提供任何密钥吗?。据我所知,这些API键只用于从浏览器运行的脚本,对吗?对不起,我完全不理解你的评论。浏览器应用程序是从浏览器运行的脚本。