无法让Ajax与Angular合作

Unable to get Ajax to work with Angular

本文关键字:Angular 合作 Ajax      更新时间:2023-09-26

我是Angular的新手。

我正在尝试使用 AJAX 调用获得 Angular 工作。

当我通过浏览器或任何 REST 客户端访问http://localhost:9091/jerseyservlet/jerseyrest/org/orgId/DEY时,我得到以下信息:

<users>
    <user>
        <id>0</id>
        <name>User 0</name>
        <owningorgid>0</owningorgid>
        <position>Test User</position>
        <email>user0@org.com</email>
        <suspended>false</suspended>
        <locked>false</locked>
        </user>
</users>

当我执行他的程序时,我总是在控制台上打印request rejected

我的角度代码如下:

我从这里复制了代码

//Creating an Angular Module named NeptuneDemo by registering the name with Angular Module constructor.
var neptuneDemo=angular.module('NeptuneDemo', []);
neptuneDemo.factory('simpleFactory',['$http','$q',function ($http,$q){
    return{
            sendRequest: function(){
                var deffered = $q.defer();
                $http
                    .get('http://localhost:9091/jerseyservlet/jerseyrest/org/orgId/DEY')
                    .success(function(xml,status,headers,config){
                        deffered.resolve(xml);
                    })
                    .error(function(xml,status,headers,config){
                        deffered.reject(xml);
                    });
                return deffered.promise;
            }
        }
    }]);
//Registering NeptuneDemoController with the Angular Module Object neptuneDemo.
neptuneDemo.controller('NeptuneDemoController',['simpleFactory','$scope',function NeptuneDemoController(simpleFactory,$scope){
    //defining the displayMessage() method mapped to the input form
    $scope.displayMessage = function(){
        $scope.users= [];
        simpleFactory.sendRequest()
        .then(function(data){
            console.log('request successful');
            console.log(data);
        },function (data){
            console.log('request rejected');
            console.log(data);
        });
    };

    //Defining New Model Object msg
    function User(id,name,owningOrgId,position,email,suspended,locked){
    this.id=id;
    this.name=name;
    this.owningOrgId=owningOrgId;
    this.position=position;
    this.email=email;
    this.suspended=suspended;
    this.locked=locked;
    }
}]);

问题 2 :我还需要解析 XML 并填充到对象的帮助。不确定jQuery.parseXML()是否可以工作。请让我知道任何更好的选择。

感谢您阅读很长的帖子。

Khanh TO可能是正确的,因为它听起来像是跨源问题。由于您的 ajax 请求中有完整的 URL,表明您正在跨越源边界,并且您需要通过少数几种方式之一进行处理 优秀的 Mozilla CORS 文章。

就使用jquery.parseXML()而言,它应该可以正常工作。内置的角度jquery可能没有它,所以你最终会添加它。