繁体   English   中英

如何从angularjs上的xml响应中获取元素值

[英]How to get an element value from xml response on angularjs

试图在我使用angularjs的客户端web应用程序中解析error_code标记。 我的Web服务以xml格式返回登录响应。

响应xml在response.data中。 附加的代码是登录表单的控制器。

var app = angular.module("myApp", []);
app.controller("loginController", function($scope, $http) {
 $scope.myFunc = function ()
 {         
$http.get("http://localhost:8080/LocWebService/rest/authorize/"+
                                    $scope.Name+"_"+$scope.Password)
   .then(function(response) {
    var response_code=response.data.getElementsByTagName("error_code") 
                                          [0].childNodes[0].nodeValue;   
    if (response_code=="1")
        alert("User Name is wrong.");
    if (response_code=="2")
        alert("User Password is wrong.");
    if (response_code=="0")
        alert("Login is successful.");        
});
}
});

没有错误消息。预期的行为是根据error_code显示适当的警报。

您可以使用X2JS库( https://github.com/x2js/x2js )将xml响应转换为JSON,然后将其用作普通对象。 例如:

var x2js = new X2JS();
var dataJsonFormat= x2js.xml_str2json(response.data);

您可以查看此主题以获取更多信息: 使用Javascript将XML转换为JSON(并返回)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM