3

I am trying to fetch weather information from Yahoo using XHR in a Chrome extension:

$.ajax({
    url: "https://weather.yahooapis.com/forecastrss?w=" + 250226 + "&u=c",
    dataType: 'xml',
    success: function(data) {
        console.log(data);
    }
});

and I have requested the permission to cross-origin using this script:

$("button").click(function(){
    chrome.permissions.request({
        origins: ['*://weather.yahooapis.com/*']
    }, function(granted) {
        if (granted) {
            console.log("Success creating permission.");   //successful
    } else {
            console.log("Not successful.");
    }
});

However, it still gives me an error saying:

XMLHttpRequest cannot load http://weather.yahooapis.com/forecastrss?w=2502265&u=c. Origin chrome-extension://randomid is not allowed by Access-Control-Allow-Origin.

enter image description here

And I can't think of any reason why this is happening. Any idea?

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • I'm not much of a web developer, [but the documentation](http://developer.chrome.com/extensions/permissions.html) doesn't seem to specify that wildcards are okay. – ta.speot.is Feb 20 '13 at 04:19
  • @ta.speot.is - http://developer.chrome.com/extensions/declare_permissions.html – Derek 朕會功夫 Feb 20 '13 at 04:20
  • I probably can't contribute any more (not a web developer etc) but the documentation that says "match patterns" seems to be for `permissions` not [`origins`](http://developer.chrome.com/extensions/permissions.html#apiReference). – ta.speot.is Feb 20 '13 at 04:22
  • @ta.speot.is - "match patterns" is for `origins`, because only the a list of permissions can be put in `permissions`. Even I removed `*` it is still not working. :( – Derek 朕會功夫 Feb 20 '13 at 04:28
  • Considering [this sample extension by Google](http://developer.chrome.com/extensions/examples/api/permissions/extension-questions/options.js), could you give `http://weather.yahooapis.com/` (without `*`) a try? – Passerby Feb 20 '13 at 04:37
  • @Passerby - Your link really helped me a lot... I tried `chrome.permissions.contains` to see if I actually have the permission and it returns `false`. After another careful look in my codes I found out that I have forgotten to put `"`s between the different permissions I already have before. Silly me. Now it works fine. – Derek 朕會功夫 Feb 20 '13 at 04:49

1 Answers1

0

Does your Chrome-Extention have a manifest file? It seems like this is the problem, you should also set your cross-origin permission there.

Seems that the randomId mention is related to the extension not being assigned a proper extension ID.

joseeight
  • 904
  • 7
  • 10
  • Hi joseeight, thanks for your answer, but I have found out where the problem is at already... I mentioned it in the comment section above. Also `random_id` is just a place holder I put in so that there won't be weird characters all over the question. – Derek 朕會功夫 Feb 21 '13 at 04:25