【问题标题】:Handling AJAX Origin errors, caused by the Chrome Extension处理由 Chrome 扩展程序引起的 AJAX Origin 错误
【发布时间】:2013-01-20 22:46:45
【问题描述】:

有没有办法处理来自 Chrome 扩展的 AJAX Origin 错误?

拒绝加载 铬扩展://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”,错误信息为空。

【问题讨论】:

    标签: ajax google-chrome jquery google-chrome-extension


    【解决方案1】:

    您可以尝试使用 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);
        }
    });
    

    【讨论】:

    【解决方案2】:

    听起来您正试图从内容脚本加载此资源,基于错误。

    您必须通过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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 2014-08-05
      • 2019-11-17
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      相关资源
      最近更新 更多