0

I am writing a web app in Angular, this piece of code is meant to get test XML data as text from the server. I am using Angular's $http service to send a GET request from the server. The variable 'xmlData' is being changed inside the $http block, but outside that block its value is still the default value. Here's the code:

this.getData = function() {
        var xmlData = "Default";
        var xmlDoc;
        // send an http GET request for the XML text data

        $http.get('http://localhost:1337/testXML.txt').success(function(data) {
            xmlData = data;
            if (xmlData == null) {
                alert("Data Error Occurred");
            }
        // here, 'xmlData' contains the correct information.
        }).
        error(function() {
            alert("HTTP Error Occurred");
        });

        // once the above $http block ends, the xmlData has the default value.          
        return xmlData;
}

So is this just a silly scope issue? Or am I using Angular's $http service incorrectly? Thanks!

Emma
  • 35
  • 2
  • 11

1 Answers1

0

I don't think you can directly insert XML like you can JSON without some sort of parser.

Check out this question for handling XML in AngularJS: How to handle XML services in AngularJS?

Specifically check out these modules/plugins to help:

Community
  • 1
  • 1
Will M
  • 2,135
  • 4
  • 22
  • 32
  • Okay, thanks! I follow this code with a parser that turns the xml data into a DOM that javascript can work with. But are you saying that the http request can't handle xml? – Emma Jul 11 '13 at 21:23
  • You can grab anything with $http. But it's only JSON that can natively be attached to a variable like you've set up. – Will M Jul 11 '13 at 21:25