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
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,
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;
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With