AngularJs 从 xml 到 json

AngularJs from xml to json

我在使用 angularjs 将 Web 服务的响应从 xml 转换为 json 时遇到问题。这是我的代码:

<!DOCTYPE html>
    <html>
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
     <script type="text/javascript" src="http://demos.amitavroy.com/learningci/assets/js/xml2json.js" charset="UTF-8"></script>
    <body>

    <div ng-app="myApp" ng-controller="customersCtrl"> 

    <ul>
      <li ng-repeat="x in names">
        {{ x.theme }}
      </li>
    </ul>

    </div>

    <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope, $http) {
      $http.get("http://brickset.com/webservices/brickset.asmx/listThemes?")
      .success(function (response) {
     $scope.names = [];
    var x2js = new X2JS();
     var json = x2js.xml_str2json(response);
    $scope.names = json.ArrayOfThemeData.themeData;
    });
    });
    </script>

    </body>
    </html>

我在视图中看不到列表 你能帮帮我吗?

您无法使用 XHR 请求访问该请求,因为服务器不允许 CORS 请求。该服务未设置 Access-Control-Allow-Origin header,因此请求被阻止。

这可能是故意的,因为 API 文档 available here 要求使用 API 密钥或用户名和密码组合对请求进行身份验证。

如果您有调用 API 的有效凭据,那么直接在服务中使用 XHR 将意味着在您的客户端代码中公开这些凭据,这可能是不安全的,这可能是API 不允许 XHR 访问。

相反,API 必须使用您设置的代理服务来访问。然后,您的服务器将使用您的 API 键查询他们的 API。

您可以通过尝试通过 this CORS testing site

访问资源来确认不允许 XHR 请求

See here 了解如何在 Ionic 中处理 CORS 问题。