簡體   English   中英

如何從angularjs上的xml響應中獲取元素值

[英]How to get an element value from xml response on angularjs

試圖在我使用angularjs的客戶端web應用程序中解析error_code標記。 我的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.");        
});
}
});

沒有錯誤消息。預期的行為是根據error_code顯示適當的警報。

您可以使用X2JS庫( https://github.com/x2js/x2js )將xml響應轉換為JSON,然后將其用作普通對象。 例如:

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

您可以查看此主題以獲取更多信息: 使用Javascript將XML轉換為JSON(並返回)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM