8

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

2 Answers 2

3

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,

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

Comments

1

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;
  });
});

5 Comments

This works if the xml is local. Since it is a cross domain request I get the "localhost is not allowed by access-control-allow-origin" error. So I need to use something like jsonp but for xml. Thanks
If you are trying to do a crossdomain request via localhost in Chrome, it will fail. You need to open Chrome with security disabled (open -a Google\ Chrome --args --disable-web-security). Otherwise, you need to make sure the service has been properly configured for CORS or you can continue down the JSONP route. I will attempt to get JSONP working in the demo.
I second the CORS comment.
@moderndegree- Thanks, I dont think disabling it in chrome is an option, I need it to work for everyone. There isn't much documentation on the API but I am sending an email to the support team to ask about the CORS. I am going to take a guess and say that its probably not configured for CORS though. Thanks,
well... in that case, I would propose creating a proxy service on your own server and then transform the response server-side.

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.