2

I'm writing a Chrome extension which performs a simple ajax call (based on this example from the docs):

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

The request always fails, because the alert shows: FAILURE: [object Object].
The URL is valid: When I put http://www.flags.99k.org/getFlags.php in my address bar, I get this:

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

Here is my manifest.json for the extension.

{
  "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/"
  ]
}

I use Chromium 17.0.963.79 (Developer Build 125985 Linux) Ubuntu 11.10.

Rob W
  • 341,306
  • 83
  • 791
  • 678
Shawn
  • 10,931
  • 18
  • 81
  • 126
  • This might help: http://stackoverflow.com/questions/6792878/jquery-ajax-error-function – Mike Christensen Mar 30 '12 at 16:51
  • How is the JavaScript code executed? It looks like [injected](http://stackoverflow.com/a/9916089/938089?chrome-extension-auto-run-a-function). What Chrome version are you using? What's the contents of `manifest.json`? – Rob W Mar 30 '12 at 16:55
  • @MikeChristensen The information I can get from the error function isn't very helpful: `readyState: 0, responseText: "", status: 0, statusText: "error"`... – Shawn Mar 30 '12 at 17:52
  • @RobW The code is run the same way as in the `getstarted` example (http://code.google.com/chrome/extensions/getstarted.html), from a script tag in the head of my `default_popup` page. But I've managed reproducing the problem in jsfiddle (see my edit). – Shawn Mar 30 '12 at 17:59

3 Answers3

5

Currently, only the root directory is allowed to be accessed, via the permissions in the manifest. You have to add a wildcard after the URI to the permissions:

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

See also: Match patterns.

EDIT: The updated code did not work, because http://www.flags.99k.org/ redirects to http://flags.99k.org/ (without www). So, also whitelist this location:

  "permissions": [
    "http://flags.99k.org/*",
    "http://www.flags.99k.org/*"
  ]
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • You sneaky man! An interesting finding, yet it still doesn't solve my problem... Could it be possible that the host itself is blocking my request? – Shawn Mar 30 '12 at 19:46
  • @Shawn I've successfully tested it. Files: [**`manifest.json`**](http://pastebin.com/33GY95cy), [**popup script**](http://pastebin.com/LtPW7VXZ), [**popup page**](http://pastebin.com/cfB2wskK). and jQuery: [**`jq.js`**](http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js) (all within the same extension directory). – Rob W Mar 30 '12 at 19:54
  • Got it! My problem was that the changes I made to the manifest were not taken into account, even if I click on 'Update extensions now'. To see the results of my changes, I've had to 'Remove' my extension and re-'Load unpacked extension...'. Is this the normal? Thanks very much for your help by the way ! – Shawn Mar 30 '12 at 20:17
  • @Shawn That's certainly not normal, but the bug is familiar to me. To reliably reload a packed extension, I have to: 1) Remove the extension 2) Close `chrome://extensions/`. 3) Drag the crx file to the browser, in order to install it. <-- Packed extensions only! **>>>** For unpacked extensions, clicking on "Reload extension" is sufficient. – Rob W Mar 30 '12 at 20:19
1

Instead of doing...

alert("FAILURE: " + response);  

...do...

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

Then if you go to the console (ctrl-shift-j on windows) you'll see an object that you can expand to see all its properties and what not, that can really help. Plus have you looked at the console when doing the request? You might get some error messages there.

PAEz
  • 8,366
  • 2
  • 34
  • 27
  • Thanks, the content of the object were of no use though: `readyState: 0, responseText: "", status: 0, statusText: "error"`... – Shawn Mar 30 '12 at 17:50
0

This could be a cross domain issue.

When you enter the URL into your address bar it works because it's being delivered to your browser from the host.

Because you are attempting to hit a full URL outside of your hosted domain - you may need to use JSONP.

msponagle
  • 330
  • 1
  • 11
  • As its a Chrome extension if he wants to make cross domain calls he can aslong as he puts the urls he needs in the permissions part of his manifest. – PAEz Mar 30 '12 at 17:26