无法让Ajax与Angular合作
Unable to get Ajax to work with Angular
我是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可能没有它,所以你最终会添加它。
相关文章:
- Angular JS IE9 Hashbang url rewriting
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 使用angular重定向到html页面
- angular.js没有'无法在PhoneGap中处理视图标记
- Javascript(Angular)从一个对象数组到第二个数组查找值
- angular 1.5应用程序中的导航栏
- angular的下拉菜单
- Angular只从数组中获取所需的数据
- 如何将不可变的js导入angular 2(alpha)
- Angular js-返回一个包含类似
- 如何使用 Angular JS 将数据保存在数据库中
- 将JSON对象传递给angular指令
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- Angular:更新一次性绑定的数据
- 同步调用,直到用户通过angular验证为访问者
- 将Angular js与taglib结合使用
- 无法让Ajax与Angular合作
- BaaS将与Angular.js合作