10

I've an AngularJS application in which I'm trying to get an XML data with $http get from a server say http://example.com/a/b/c/d/getDetails?fname=abc&lname=def (this when accessed manually by entering the link in a browser shows a tree structure of the XML file).

When I run the application the data from that link is not fetched. Instead its showing an error with status 0.

//url = http://example.com/a/b/c/d/getDetails?fname=abc&lname=def
$http.get(url).success(function (data){
                alert("Success");
                deferred.resolve(data);
            }).error(function (data, status){
                console.log("Error status : " + status);
            });

I'm not sure why the $http.get fails and goes to the error function returning status 0.

But if I pass an URL to the local server which has a JSON object, it works.

Is the problem because of me trying to access the XML file from a different domain(CORS issue?) or something else?

Please help me out!

3
  • Yes, See your console, you should have got an error (Cross Origin ...) Commented Nov 26, 2014 at 9:21
  • 1
    Nope. I haven't got anything like that in my console. Commented Nov 26, 2014 at 9:25
  • What if I don't want to console log the error but return it to the DOM? Commented Oct 15, 2015 at 8:33

2 Answers 2

17

You have HTTP access control (CORS) issues .

The server answering your REST requests MUST include the headers specified by CORS in order to allow Angular to consume properly the response. Essentially this means including the Access-Control-Allow-Origin header in your response, specifying the servers from where the request comes from, that are allowed. (ref)

There is directive Angular allows one to get/set the whitelists and blacklists used to ensure that the URLs used for sourcing Angular templates are safe.

Sign up to request clarification or add additional context in comments.

2 Comments

So will it be enough if I whitelist the link I use to access the XML data?
If you use xmlhttprequests you need JSONpadding. However, the server must supports "Cross-Origin Resource Sharing (CORS)" in this can it's quiet simple to receive data from a other domain.
2

yes you got it right, its because default content-type for $http is “application/json” if you want to change the request data or response data you can change it with the help of tranformRequest and tranformResponse. you can find more information on it here

also i find an article on implementation of tranformRequest change post data from json to form post in angularjs

2 Comments

Thanks. So for my application after using transformRequest, should I use xml2json to convert the xml content to JSON and use it? (rabidgadfly.com/2013/02/angular-and-xml-no-problem) or is there any other better solution to this?
your link is redirecting to spam sites

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.