【发布时间】:2017-01-18 15:07:58
【问题描述】:
错误:0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“setApiKey”
您好,我正在拼命尝试让它在我的通用 Windows 应用程序上运行。有一个 Google 网址缩短器示例,其中包含有关如何使用 API 的说明。
https://developers.google.com/api-client-library/javascript/samples/samples
当我在我的浏览器中运行它时,所有这些都有效,但是一旦我开始在我的通用 Windows 应用程序中运行它,它就不起作用了。我认为这与 UWP 的安全性有关,不允许使用内联脚本,并且您无法正常从 Web 加载脚本。你必须使用 webview 从 web 加载脚本,所以我用这个 webview 做到了这一点:
<x-ms-webview id="UrlShortenerWebview"src="ms-appx-web:///Pages/URLShortener/URLShortener.html" style="width: 200px; height: 200px; border: 1px solid black;"></x-ms-webview>
这是我的 URL Shortener html 文件:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="https://apis.google.com/js/api.js" type="text/javascript"></script>
<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('AIzaSyCzBnER6KmLiO2ZBIycZIPCEQEXxIrHnR0');
gapi.client.load('urlshortener', 'v1').then(makeRequest)
}
gapi.load("client", init);
</script>
</head>
<body>
<div id="results"></div>
</body>
</html>
错误:0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“setApiKey”
我不知道为什么会发生这种情况,也不知道我还能做些什么来解决这个问题。甚至可以在windows应用程序中使用google api???
【问题讨论】:
-
这本质上是一个 NPE。调试您的代码并找出
null是如何进入您的代码的。 -
我不知道怎么做。当我控制台日志 gapi.client 原来是未定义的。是因为 gapi.load 函数,没有创建客户端对象吗?即使我不使用 gapi.load 函数而只使用 ?onload=init 加载 api,我也会收到相同的错误消息
标签: javascript windows google-api win-universal-app uwp