首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从angularjs上的xml响应中获取元素值

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

Stack Overflow用户
提问于 2019-05-16 03:47:59
回答 1查看 691关注 0票数 0

尝试在我的客户端web应用程序中解析error_code标签,它使用了angularjs。我的web服务以xml格式返回登录响应。

响应xml为response.data格式。所附代码是用于登录表单的控制器。

代码语言:javascript
复制
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是根据error_code显示相应的警报。

EN

回答 1

Stack Overflow用户

发布于 2019-05-16 06:02:10

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

代码语言:javascript
复制
var x2js = new X2JS();
var dataJsonFormat= x2js.xml_str2json(response.data);

你可以查看这个帖子了解更多信息:Convert XML to JSON (and back) using Javascript

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56156628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档