这是在扩展程序中运行的代码,位于 background.html 中。
function onLoad()
{
var path="http://localhost:8082/index.htm?key=1234";
xhr.open('get', path, false);
xhr.send();
token=xhr.responseText;
caughtMsg=token+" *** ";
var channel = new goog.appengine.Channel(token);
var handler = {
'onopen': function(){caughtMsg+=" *** open";},
'onmessage': onMessage,
'onerror': function(e){caughtMsg+=" *** err "+e.description+" "+e.code},
'onclose': function(){caughtMsg+=" *** close";}
};
var socket = channel.open(handler);
socket.onmessage = onMessage;
}
它与创建通道并返回令牌的 localhost 服务器脚本(在 python 中)交互。然后令牌用于在客户端创建通道对象。相反,我从汇总响应中得到的是:
通道 354645736-1234 * 错误无效+令牌 401 *关闭
当我在扩展范围之外执行此操作时(不使用 ajax 建立连接),它就像一个魅力。
我会误入歧途吗?