[英]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. 试图在我使用angularjs的客户端web应用程序中解析error_code标记。 My web service returns the login response in xml format.
我的Web服务以xml格式返回登录响应。
response xml is in response.data. 响应xml在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. 没有错误消息。预期的行为是根据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. 您可以使用X2JS库( https://github.com/x2js/x2js )将xml响应转换为JSON,然后将其用作普通对象。 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 您可以查看此主题以获取更多信息: 使用Javascript将XML转换为JSON(并返回)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.