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.
我正在尝试使用AngularJs连接到answerbase api。我得到它使用jsonp返回xml但我得到格式错误。不幸的是,xml是他们将返回的唯一格式。我试图使用xml2json库但没有成功。这就是我到目前为止所拥有的。
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 个解决方案
#1
3
Same here. Unfortunately the jsonp function parses the content BEFORE the transform callback. So no, it's not possible.
同样在这里。不幸的是,jsonp函数在转换回调之前解析内容。所以不,这是不可能的。
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)
您只能将响应更改为JSON或使用其他方法。 (好吧,你也可以在你的服务器上包装真正的API并以JSON格式生成相同的结果,构建镜像API服务)
Have a nice day,
祝你今天愉快,
#2
1
Without access to the same service you are using, it is hard to pinpoint the issue. Are you using a public API?
如果无法访问您正在使用的相同服务,则很难确定问题。您使用的是公共API吗?
I was able to get basic demo to work using $http.get
. Take a look at this plunker.
我能够使用$ http.get获得基本的演示。看看这个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;
});
});
#1
3
Same here. Unfortunately the jsonp function parses the content BEFORE the transform callback. So no, it's not possible.
同样在这里。不幸的是,jsonp函数在转换回调之前解析内容。所以不,这是不可能的。
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)
您只能将响应更改为JSON或使用其他方法。 (好吧,你也可以在你的服务器上包装真正的API并以JSON格式生成相同的结果,构建镜像API服务)
Have a nice day,
祝你今天愉快,
#2
1
Without access to the same service you are using, it is hard to pinpoint the issue. Are you using a public API?
如果无法访问您正在使用的相同服务,则很难确定问题。您使用的是公共API吗?
I was able to get basic demo to work using $http.get
. Take a look at this plunker.
我能够使用$ http.get获得基本的演示。看看这个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;
});
});