【问题标题】:Use of aJax in extension default_popup?在扩展 default_popup 中使用 aJax?
【发布时间】:2012-12-30 23:12:51
【问题描述】:

我可以在我的 Google Chrome 扩展程序“default_popup”中以某种方式使用 aJax。

我已经尝试了以下...

manifest.json:

{
  "name": "My extension",
  "manifest_version": 2,
  "version": "1.0",
  "permissions": [
    "tabs", "http://*/*"
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["jquery-1.8.3.min.js", "content.js"],
      "run_at": "document_end"
    }
  ]
}

popup.html:

<html>
<body>
<script>
$.ajax({
    type: "GET",
    url: "http://www.mysite.com/api.php"
}).done(function(response) {
    alert("work");
}).fail(function(response) {
    alert("doesn't work");
});
</script>
</body>
</html>

有人知道怎么做吗?

【问题讨论】:

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


    【解决方案1】:

    这是可能的,但您很可能必须使用JSONP。您可以使用已有的内容作为基础,因此不需要太多重构。

    【讨论】:

    • 感谢您的提示,但我认为 Chrome 不支持 JSONP 扩展。根据我的经验,我只是尝试将其转换为 JSONP,但后来 Chrome 无法加载它。但我可能做错了。
    • 我很确定他们会这样做,因为我自己使用 JSONP 进行扩展。我不能肯定地说 100%,因为我在扩展程序方面遇到了另一个问题,但根据我的研究,如果您尝试向安全域 (https) 提出请求,应该是可能的。在这里读一点:groups.google.com/a/chromium.org/group/chromium-extensions/tree/…
    • 另一个,最好在这里阅读:stackoverflow.com/questions/8495825/…
    • 我现在设法使它工作,通过使用“XMLHttpRequest”而不是 jQuery aJax。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-05-30
    • 2022-11-15
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    相关资源
    最近更新 更多