3
這裏是在擴展中運行的代碼,位於background.html中。使用Chrome擴展中的通道Google應用引擎
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;
}
它與創建通道並返回令牌的本地主機服務器腳本(在Python中)交互。那麼令牌用於在客戶端創建通道對象。 相反,我從聚合反應得到的是:
通道354645736-1234 * ERR無效+令牌401 *接近
當我做到這一點的延伸範圍之外(不使用AJAX建立連接)它像一個魅力。
我在哪裏誤入歧途?
你確定localhost正在返回一個有效的令牌嗎? – abraham
我很確定這個令牌是有效的。正如我寫的,當我創建不在鉻擴展上下文中的令牌並且不使用ajax(像通道API正式文檔建議的那樣)時,它工作得很好。 – shayuna
嘗試獲取通過擴展生成的令牌,並確保它在擴展上下文之外工作。 – abraham