Chrome扩展程序中的AJAX请求失败,具有正确的清单权限?

时间:2012-03-30 16:35:57

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

我正在编写Chrome扩展程序,执行简单的ajax调用(基于文档中的this example):

$.ajax({
    type: "GET",
    url: "http://www.flags.99k.org/getFlags.php"
}).done(function(response) {
    alert("SUCCESS: " + response);
}).fail(function(response) {
    alert("FAILURE: " + response);
});

请求始终失败,因为警报显示:FAILURE: [object Object] 该网址有效:当我将http://www.flags.99k.org/getFlags.php放入我的地址栏时,我明白了:

[{"UID": "1", "Message": "Hello"}, {"UID": "2", "Message": "World"}, {"UID": "3", "Message": "Hello World"}]

这是扩展名的manifest.json。

{
  "name": "Hello World",
  "version": "1.0",
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
      "http://www.flags.99k.org/"
  ]
}

我使用Chromium 17.0.963.79(Developer Build 125985 Linux)Ubuntu 11.10。

3 个答案:

答案 0 :(得分:5)

目前,只允许通过清单中的权限访问根目录。您必须在URI 之后添加通配符到权限:

  "permissions": [
    "http://www.flags.99k.org/*"
  ]

另请参阅:Match patterns

编辑:更新的代码无效,因为http://www.flags.99k.org/重定向到http://flags.99k.org/(没有www)。所以,还要将此位置列入白名单:

  "permissions": [
    "http://flags.99k.org/*",
    "http://www.flags.99k.org/*"
  ]

答案 1 :(得分:1)

而不是......

alert("FAILURE: " + response);  

......做......

console.debug('FAILURE');
console.debug(response);

然后,如果你去控制台(Windows上的ctrl-shift-j),你会看到一个可以展开的对象,看看它的所有属性,什么不是,这真的有帮助。另外,您在执行请求时是否看过控制台?你可能会收到一些错误消息。

答案 2 :(得分:0)

这可能是一个跨域问题。

当您在地址栏中输入网址时,它会正常运行,因为它会从主机传送到您的浏览器。

因为您试图访问托管域之外的完整网址 - 您可能需要使用JSONP。