Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross domain ajax request that returns xml using AngularJS

I am trying to connect to the answerbase api using AngularJs. I got it to return the xml using jsonp but I get a format error. Unfortunately xml is the only format they will return. I have attempted to use the xml2json library with no success. This is what I have so far.

var url = "http://myAnswerBaseSite.com/api/getquestionslist.aspx?apikey=myKey&callback=JSON_CALLBACK";

$http.jsonp(url,
    {
        transformResponse: function (data) {
            var x2js = new X2JS();
            var json = x2js.xml_str2json(data);
            return json;
        }
    })
    .success(function (data) {
        console.log(data.found);
    });

Is this possible? Thanks in advance

like image 288
user973671 Avatar asked Aug 14 '13 19:08

user973671


2 Answers

Same here. Unfortunately the jsonp function parses the content BEFORE the transform callback. So no, it's not possible.

You can only change the response to be a JSON or use another method. (Well, you can also wrap the real API on your server and produce the same results in JSON format, building a mirror API service)

Have a nice day,

like image 81
mircobabini Avatar answered Nov 15 '22 04:11

mircobabini


Without access to the same service you are using, it is hard to pinpoint the issue. Are you using a public API?

I was able to get basic demo to work using $http.get. Take a look at this plunker.

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $http) {
  $scope.name = 'World';
  var url = "data.xml";
  $http.get(url,{
    transformResponse: function (data) {
      var x2js = new X2JS();
      var json = x2js.xml_str2json(data);
      return json;
    }
  }).success(function (data) {
    $scope.foo = data.foo;
  });
});
like image 36
Brian Lewis Avatar answered Nov 15 '22 04:11

Brian Lewis