0

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.

dorj bayar
  • 53
  • 8
  • 1
    Hi, i think you need to parse your XML string before navigating through it using : parser = new DOMParser(); xmlDoc = parser.parseFromString(txt, "text/xml");...check https://stackoverflow.com/questions/17604071/parse-xml-using-javascript – bloo May 15 '19 at 19:59

1 Answers1

0

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

Daria
  • 66
  • 1
  • 4