尝试在我的客户端web应用程序中解析error_code标签,它使用了angularjs。我的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.");
});
}
});
No error messages.Expected behavior是根据error_code显示相应的警报。
发布于 2019-05-16 06:02:10
您可以使用XML库(https://github.com/x2js/x2js)将X2JS响应转换为https://github.com/x2js/x2js,然后将其用作普通对象。例如:
var x2js = new X2JS();
var dataJsonFormat= x2js.xml_str2json(response.data);
你可以查看这个帖子了解更多信息:Convert XML to JSON (and back) using Javascript
https://stackoverflow.com/questions/56156628
复制相似问题