【问题标题】:Unable to get Ajax to work with Angular无法让 Ajax 与 Angular 一起工作
【发布时间】:2013-10-11 10:02:56
【问题描述】:

我是 Angular 的新手。

我正在尝试让 Angular 使用 AJAX 调用。

当我通过浏览器或任何 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

我的 Angular 代码如下:

我已经从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;
    }
}]);

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

感谢您阅读这篇非常长的帖子。

【问题讨论】:

  • 您的 REST 服务是否支持内容协商?你能返回 JSON 而不是 xml 吗?此外,您不需要将 $http 包装在一个 Promise 中,因为它已经返回一个。
  • @DavinTryon 现在该服务只返回 XML,因为开发人员对 XML 有信心。我将$http 包装在承诺中,因为我提到的答案就是这样做的。我对 jQuery XHR 很满意,但目前正在阅读角度指南。
  • 我测试了您的代码,发现它运行良好。问题可能是由于跨域造成的。

标签: javascript jquery ajax angularjs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2020-02-29
    • 2011-04-25
    • 2014-11-22
    • 2014-05-15
    相关资源
    最近更新 更多