AngularJS获取rest xml数据

时间:2017-09-26 09:53:06

标签: angularjs spring spring-rest

我上面有一个类,这个方法返回一个xml结果。

@RestController
@RequestMapping("api/")
public class RateController {
    @RequestMapping(value = "/currencies/{ids}")
    public Currency getRates(@PathVariable String ids) throws JAXBException, IOException {
        JAXBContext jc = JAXBContext.newInstance(Currency.class);
//...
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(currency, System.out);
        return currency;
    }
}

然后我尝试从angularjs中取出它,但我无法获取数据。如何在angularJS中获取xml formate中的数据?这种方法就在上面。

var CurrencyController = function($http, $scope, $routeParams, exampleSvc) {
    var getCurrencies = function(data) {
        var result = $http.get("/api/currencies/" + $scope.ids);
        var x2js = new X2JS();
        var json = x2js.xml_str2json(result);
        onCurrencyComplete(json);
    };
    var onCurrencyComplete = function(response) {
        $scope.currencies = response.data;
    };

1 个答案:

答案 0 :(得分:0)

你应该在getCurrecies中执行以下操作:

var getCurrencies = function(data) {
        $http.get("/api/currencies/" + $scope.ids).then(function(response){
             var result = response.data
             var x2js = new X2JS();
             var json = x2js.xml_str2json(result);
             onCurrencyComplete(json);
        });
};