3

BLUF: Running AJAX call from Chrome extension isn't working properly.

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'm trying to get weather data from a site that requires a GET to request the data XML. They give tips for building the request string for the URL, and upon pasting the URL in the address bar, the appropriate XML data displays correctly.

Next, I assembled the AJAX call and ran it, but nothing seems to happen. I changed the function for the success property to simply 'alert()' and ran the extension again, and the alert box popped up.

The fact that the alert is showing up means that the call was successful, unless i'm totally out to lunch ... so why isn't the original function working? I realize that the code searching the XML might not be right, but placing an alert right before I start dealing with the doc isn't popping up either, which tells me for some reason it's not entering the function at all.

3
  • Did you check for Same Origin Policy violations? en.wikipedia.org/wiki/Same_origin_policy Commented Feb 7, 2013 at 17:42
  • Did you change it like this success: function() {alert() } or like this success: alert()? The first one means your call is working; the second does not mean that, because alert will be called right away. Commented Feb 7, 2013 at 17:44
  • Please look at the cross domain permission section of the extension faq developer.chrome.com/extensions/xhr.html Commented Feb 7, 2013 at 17:45

2 Answers 2

1

It may because of the Same Origin Policy constraints of the AJAX requests. Chrome allows you to make cross domain ajax request in the extensions but we need to add permissions to the extension manifest file as given in the document

Looks like you may have to add cross domain access permission to the manifest file like

{
  "name": "My extension",
  ...
  "permissions": [
    "http://weather.aero/"
  ],
  ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

that worked perfectly! I knew I had to be able to access the server request that they were offering, but I had overlooked the Chrome permissions requirement. Adding that fixed the problem, thanks!
0

I think you should do it in success function:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.