简体   繁体   中英

How to get an element value from xml response on angularjs

Trying to parse error_code tag in my client web app which uses angularjs. My web service returns the login response in xml format.

response xml is in response.data. The attached code is controller for login form.

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

No error messages.Expected behavior is that appropriate alert is displayed according to error_code.

You can use X2JS library ( https://github.com/x2js/x2js ) to convert the xml response into JSON and then use it as a normal object. for example:

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

you can check this thread for more info: Convert XML to JSON (and back) using Javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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