【发布时间】:2011-11-01 01:35:07
【问题描述】:
我在 Chrome 扩展程序中使用 Channel API。
在Google App Engine Channel API Javascript Reference (Python) 页面上说
在任何 JavaScript 代码之前在您的 html 页面中包含以下内容 那是指它:
<script type="text/javascript" src="/_ah/channel/jsapi"></script>
所以,我把它放在我的 options.html 文件的标题中:
<html>
<head>
<title>Extension Options</title>
<script type="text/javascript" src="/_ah/channel/jsapi"></script>
</head>
但 Chrome 会抛出 jsapiFailed to load resource 错误。我做错了什么?
更新
根据 Moishe 的回答,我更新了对 jsapi 的调用,如下所示:
<head>
<title>Extension Options</title>
<!-- this does not work because it is local
<script type="text/javascript" src="/_ah/channel/jsapi"></script>
-->
<script type="text/javascript" src="https://talkgadget.google.com/talkgadget/channel.js"></script>
</head>
更新
我添加了onopen 和其他属性。现在我收到onopen 警报,但我没有收到evt.data 警报。我做错了什么?
<html>
<head>
<title>Extension Options</title>
<!-- this does not work because it is local url
<script type="text/javascript" src="/_ah/channel/jsapi"></script>
-->
<script type="text/javascript" src="https://talkgadget.google.com/talkgadget/channel.js"></script>
</head>
<body>
<p>Enter your gmail address:</p>
<textarea id="getEmail" style="margin-bottom: 4px; width: 250px; height: 20px">
</textarea><br />
<button id="save">Save</button>
<!--<button id="save">Clear</button>-->
<script>
document.getElementById("getEmail").placeholder = "your gmail address" ;
//save entered gmail address
document.getElementById("save").addEventListener
(
"click",
function ()
{
var userEmail = document.getElementById("getEmail").value;
var formData = new FormData();
formData.append("extension_user", userEmail);
alert("after formData.append")
var channel;
var socket;
var handler =
{
onopen: function () { alert("onopen") },
onerror: function () { alert("onerror") },
onclose: function () { alert("onclose") },
onmessage:
function (evt)
{
//evt.data will be what the server sends in channel.send_message
console.log("evt.data received from authhandler: " + evt.data);
alert("evt.data is: " + evt.data)
}
};
var xhr = new XMLHttpRequest();
//changed to lowercase
xhr.onreadystatechange = function()
{
//alert("xhr.onReadyStateChange")
//error handling etc not included
if (xhr.readyState == 4 && xhr.status == 200)
{
token = xhr.responseText;
alert("token: " + token)
channel = new goog.appengine.Channel(token);
socket = channel.open(handler);
}
};
xhr.open("POST", "http://ting-1.appspot.com/authsender", true);
xhr.send(formData);
console.log("formData sent to authsender: " + formData);
}, false
)
</script>
</body>
</html>
【问题讨论】:
-
这是使用 dev_appserver 还是在生产中?您能否提供有关 Chrome 开发工具中“网络”标签的任何详细信息?
-
这是在生产中。首先,“网络”选项卡中没有任何内容;然后我按照指示重新加载页面,我看到 2 个项目:options.html 和 jsapi:名称:options.html;方法:获取;状态:(来自缓存);类型:文本/html;大小:(来自缓存);时间:3ms 和
jsapi /_ah/channel; GET, (failed); undefined; 30B; 5ms谢谢! -
你介意告诉我你的appid吗?如果您尝试直接导航到“your-app-id.appspot.com/_ah/channel/jsapi”会发生什么?
-
确定它是 ting-1,所以当我粘贴上面的应用程序 ID 时,它会重定向到 talkgadget.google.com/talkgadget/channel.js,页面上有一堆代码
-
如果它直接在浏览器中运行,那么您的应用对 /_ah/channel/jsapi 的“options.html”请求有何不同? options.html 是否托管在 ting-1.appspot.com 上? (当我尝试加载它时,我得到了 404)
标签: google-app-engine google-chrome google-chrome-extension