如何写angularJs的Controler从Parse.com得到休息数据 [英] How to write an angularJs Controler to GET Rest Data from Parse.com
问题描述
请参见下面的解决方案:
我想从对象值连接到Parse.com休息后端和显示数据。
I'm trying to connect to a Parse.com Rest backend and display data from object values.
HTML(我把几个角度呼吁一定要赶输出):
HTML (I put several angular calls to be sure to catch output):
<div ng-controller="MyController">
<p>{{item}}<p>
<p>{{items}}<p>
<p>{{item.firstName}}<p>
<p>{{data}}<p>
</div>
JAVASCRIPT休息:
JAVASCRIPT rest:
function MyController($scope, $http) {
$scope.items = [];
$scope.getItems = function() {
$http({method : 'GET',url : 'https://api.parse.com/1/classes/Professional/id', headers: { 'X-Parse-Application-Id':'XXXX', 'X-Parse-REST-API-Key':'YYYY'}})
.success(function(data, status) {
$scope.items = data;
})
.error(function(data, status) {
alert("Error");
});
};
}
这是行不通的,它严格什么,在控制台中甚至没有消息。
我知道REST调用了正确的凭据,因为我能够获得对象的内容返回时,我用剩下的测试程序进行测试。也许URL不该绝?
任何线索是非常可喜的,我已经花费数天时间这一点。
This won't work, it does strictly nothing, not even a message in the console. I know the rest call got the correct credential, as I'm able to get object content returned when I test it with a rest tester program. Maybe the URL should not be absolute ? Any clue is very welcome, i've spent DAYS on that.
解决方案:
由于人回答这个线索的帮助下,我能找到解决这个问题,所以我只是想回贡献:
Thanks to the help of people answering this thread, I was able to find the solution to this problem so I just wanted to contribute back:
从Parse.com后端获取JSON对象的数据,它传递鉴别参数:
Get Json object data from Parse.com backend, pass it authentification parameters:
function MyController($scope, $http) {
$scope.items = [];
$scope.getItems = function() {
$http({method : 'GET',url : 'https://api.parse.com/1/classes/Professional', headers: { 'X-Parse-Application-Id':'XXX', 'X-Parse-REST-API-Key':'YYY'}})
.success(function(data, status) {
$scope.items = data;
})
.error(function(data, status) {
alert("Error");
});
};
请注意,'必要角落找寻头键对象。那些''不在身边方法和url键必要的。
Notice that ' ' necessary arround header key object values. Those ' ' are not necessary around method and url keys.
模板,它列出了每个对象的所有'的firstName:
Template that list all 'firstName' of each object:
<div ng-controller="MyController" ng-init="getItems()">
<ul>
<li ng-repeat="item in items.results"> {{item.firstName}} </li>
</ul>
</div>
注意:在items.results项。 结果是必要的,因为返回的值是包含与JSON数组,列出对象结果领域的JSON对象。这可以为您节省一些头痛。
还要注意NG-INIT:如果你不把那个,或者调用getItem()时任何其他形式的,那么什么都不会发生,您将返回错误
Notice: "item in items.results". "results" is necessary because the return value is a JSON object that contains a results field with a JSON array that lists the objects. This could save you some headache. Also notice "ng-init": if you don't put that, or any other form of call to the getItem(),then nothing will happen and you will be returned no error.
这是我Angularjs的第一次尝试,我已经爱上^^。
That was my first try of Angularjs, and i'm already in love ^^.
希望这有助于
本杰明
Hope this helps, Benjamin
推荐答案
根据您的请求的控制器应该是:
Based in your request the controller should be:
HTML
<div ng-controller="MyController">
<button type="button" ng-click="getItems()">Get Items</button>
<ul>
<li ng-repeat="item in items"> item.firstName </li>
</ul>
</div>
JS
function MyController($scope, $http) {
$scope.items = []
$scope.getItems = function() {
$http({method : 'GET',url : 'https://api.parse.com/1/classes/Users', headers: { 'X-Parse-Application-Id':'XXXXXXXXXXXXX', 'X-Parse-REST-API-Key':'YYYYYYYYYYYYY'}})
.success(function(data, status) {
$scope.items = data;
})
.error(function(data, status) {
alert("Error");
)}
}
}
这篇关于如何写angularJs的Controler从Parse.com得到休息数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!