0

Can I somehow use aJax in my Google Chrome Extension "default_popup".

I have tried the following...

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>

Does anyone have any ideas how to do this?

Mark Topper
  • 339
  • 1
  • 6
  • 14

1 Answers1

0

It's possible, but you'll most likely have to use JSONP. You can use what you already have as a basis, so it shouldn't require much refactoring.

Community
  • 1
  • 1
Index
  • 2,351
  • 3
  • 33
  • 50
  • Thanks for the tip, but I don't think that Chrome support extensions with JSONP.. Just after my experience, I just tried to make it into JSONP, but then Chrome could not load it. But I could have been doing it wrong. – Mark Topper Dec 31 '12 at 00:04
  • I'm pretty sure they do as I working on a extension myself using JSONP. I can't say for sure 100% since I'm having another problem with the extension, but from what I've researched it should be possible if you attempt make your request towards a secure domain (https). A little reading here: https://groups.google.com/a/chromium.org/group/chromium-extensions/tree/browse_frm/thread/2694ca92e1b688ee/cfc4985944d8ab80?hl=en&_done=/a/chromium.org/group/chromium-extensions/browse_frm/thread/2694ca92e1b688ee/cfc4985944d8ab80?hl%3Den%26tvc%3D1%26&hl=en&tvc=1&pli=1 – Index Dec 31 '12 at 00:16
  • Another, better read here: http://stackoverflow.com/questions/8495825/jsonp-request-in-chrome-extension-callback-function-doesnt-exist/8818860#8818860 – Index Dec 31 '12 at 00:17
  • I now managed to make it work, by using the "XMLHttpRequest" instead of jQuery aJax. Thanks a lot for the help. – Mark Topper Dec 31 '12 at 10:02