2012-03-15 5 views
2

私はディスカス通知Chromeの拡張機能を作成しています。これはdisqus.comへのHTTPコールの作成を含みますが、私はAJAXコールを取得できません - Chromeは私に有名なNETWORK_ERR: XMLHttpRequest Exception 101エラーを与えます。私はどこかで読んChromeの拡張機能、クロスドメインAJAXはNETWORK_ERRを与える:XMLHttpRequestの例外101

は、ChromeはAJAXをアンパック拡張子から呼び出すクロスドメインをブロックすること(ここで思い出すことができない)ので、私はまた私の拡張パック試してみた - しかし、同じ結果を。また、バックグラウンドページ以外のどこからでもドメイン間AJAXを実行することはできません。

manifest.jsonを:

{ 
"name": "Disqus notifier", 
"version": "1.0", 
"description": "Get notifications when you have new replies on your Disqus posts", 
"browser_action": { 
    "default_icon": "icon.png", 
    "popup": "popup.html" 
}, 
"icons": { 
    "16": "icon16.png", 
    "48": "icon48.png", 
    "128": "icon128.png" 
}, 
"background_page": "background.html", 
    "permissions": [ 
     "http://*/*", 
     "https://*/*" 
    ] 
} 

background.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <script type="text/javascript" src="background.js"></script> 
</head> 
<body> 

</body> 
</html> 

background.js:

function poll() { 
    var xhr = new XMLHttpRequest(); 
    xhr.onreadystatechange = handleStateChange; // Implemented elsewhere. 
    xhr.open("GET", chrome.extension.getURL('http://disqus.com/api/3.0/messagesx/unread.json?user=<...>&api_key=<...>'), false); 
    xhr.send(null); 
    console.log(xhr.responseText); 
} 

function handleStateChange() { 
    if (this.readyState == 4) { 
     var resp = JSON.parse(this.responseText); 
     updateUi(resp); 
    } 
} 

function updateUi(json) { 
    console.log("JSON: ", json); 
} 

popup.html:

<html> 
<head> 
    <title>Disqus notifier</title> 
    <script type="text/javascript"> 
     function updateButtonClicked() { 
      chrome.extension.getBackgroundPage().poll(); 
     } 
    </script> 
</head> 

<body> 
    <button type="button" onclick="updateButtonClicked()">Update</button> 
</body> 
</html> 

xhr.send(null);ラインは101エラーを記録するものです。イベントハンドラhandleStateChangeでは、this.responseTextUnexpected end of inputで失敗するJSON.parseを引き起こして、空の文字列です。

ので:私は、クロスドメインAJAX呼び出しを行うために許可されるために何をしないのですか?

答えて

7

あなたのbackground.jsのエラーを持っている....

xhr.open("GET", chrome.extension.getURL('http://disqus.com/api/3.0/messagesx/unread.json?user=<...>&api_key=<...>'), false); 

...する必要があります..... chrome.extension.getURLがためである

xhr.open("GET", 'http://disqus.com/api/3.0/messagesx/unread.json?user=<...>&api_key=<...>', false); 

あなたの内線でファイルのURLを取得しています。
http://code.google.com/chrome/extensions/extension.html#method-getURL
あなただけの背景ページ(コンテンツ・スクリプトなど、今お使いの拡張機能での任意のページ、かなり確信して)よりも多くのXHRリクエストを行うことができます。
Chromeは、展開されていない内線番号からのxhrコールをブロックしません。私の額をピシャリと打つ私の音だった

+1

。 'getURL'呼び出しは、これまでのいくつかの実験の遺物でした。ありがとうございました! –

関連する問題