2013-02-05 45 views
0

有沒有辦法處理來自Chrome擴展的AJAX來源錯誤?處理由Chrome擴展引起的AJAX來源錯誤

拒絕 鉻擴展的負載://kldbdjcbjohfhddpicldkbifbkcdanid/data.json。 必須在web_accessible_resources清單鍵 中列出資源,以便通過擴展之外的頁面加載資源。

我嘗試這樣做:

$.ajax({ 
    url: 'chrome-extension://kldbdjcbjohfhddpicldkbifbkcdanid/data.json', 
    datatype: 'json', 
    success: function(xhr) { 
     alert('ok'); 
    }, 
    erro r: function(xhr, status, err) { 
     alert('status: ' + xhr.status + ' - ' + err); 
    } 
}); 

,但一無所獲變量。錯誤狀態爲「0」,錯誤信息爲空。

回答

0

您可以嘗試使用jsonp格式。這經常會導致交叉錯誤。

$.ajax({ 
    url: 'chrome-extension://kldbdjcbjohfhddpicldkbifbkcdanid/data.json', 
    datatype: 'jsonp', 
    success: function(xhr) { 
     alert('ok'); 
    }, 
    erro r: function(xhr, status, err) { 
     alert('status: ' + xhr.status + ' - ' + err); 
    } 
}); 
+0

沒有在狀態「0」仍然得到和「」在err –

+0

您是否查看過Google有關包含擴展程序的跨源請求的信息? http://developer.chrome.com/extensions/xhr.html –

0

聽起來就像您試圖從內容腳本中加載此資源的基礎上的錯誤。

您必須在清單中通過web_accessible_resources明確添加單個擴展資源或匹配您的擴展資源的模式。您的錯誤消息指出這是解決方案。

完成此操作後,您可以使用chrome.extension.getURL(resourcePath)構建URL。

下面是我的表現摘錄:

"web_accessible_resources" : [ 
    "*.html" 
] 

而且這裏是我使用的要求我的HTML模板文件中的代碼:

var url = chrome.extension.getURL("template.html"); 
$.get(url,function(data, textStatus, jqXHR){ 
    console.log("Got the template!"); 
    console.log(data); 
},"html");