Issue calling jQuery AJAX from Chrome extension

BLUF: Triggering an AJAX call from a Chrome extension does not work correctly.

AJAX:

$.ajax({
    type: "GET",
    url: "http://weather.aero/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=KFBG&hoursBeforeNow=1&fields=raw_text",
    dataType: "xml",
    success: function(xml){
        $(xml).find("raw_text").each(function(){
            var metar = $(this).text();
        });

        $("#ob-body").html(metar);
    }
});

      

So I am trying to get weather data from a site that requires a GET to request XML data. They give advice on how to create a query string for the URL, and when you paste the URL into the address bar, the corresponding XML data is displayed correctly.

Then I put together the AJAX call and run it, but nothing happens. I changed the function for the success property to just "alert ()" and ran the extension again and an alert box appeared.

The fact that the warning appears means the call was successful, if I don't completely leave for lunch ... so why isn't the original feature working? I understand that the code looking for XML might be wrong, but placing the warning right before I start working on the document also doesn't appear, which tells me for some reason that it is not part of the function at all.

+3


source to share


2 answers


This may be due to limitations of the same original AJAX request policy. Chrome allows you to make a cross domain ajax request on extensions, but we need to add permissions to the extension manifest file as stated in the doc

It looks like you might have to add cross-access access to your manifest file like



{
  "name": "My extension",
  ...
  "permissions": [
    "http://weather.aero/"
  ],
  ...
}

      

+2


source


I think you should do this with success:



$(xml).find("raw_text").each(function(){
     var metar = $(this).text();
     $("#ob-body").html(metar);
});

      

0


source







All Articles