0

I am new to Angular.

I am trying to get Angular work with AJAX calls.

When I access http://localhost:9091/jerseyservlet/jerseyrest/org/orgId/DEY via browser or any REST client I get the following:

<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>

When I execute he program, I always get request rejected printed on console.

My Angular Code is as contained below:

I have copied the code from here

//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;
    }
}]);

Issue 2 : I also need help in parsing the XML and populate to the object. Not Sure if jQuery.parseXML() would work. Please let me know any better options.

Thanks for reading the really long post.

4

1 回答 1

0

Khanh TO is probably correct in that it sounds like cross origin issue. Since you have full URL in your ajax request that indicates that you are crossing origin boundaries and that you will need to handle via one of few ways excellent Mozilla CORS article.

As far as using jquery.parseXML(), it should work fine. The builtin angular jquery may not have it so you would end up adding it in.

于 2013-10-11T21:11:37.397 回答