![](/img/trans.png)
[英]How to make an AngularJS service that will retrieve data from Behance API?
[英]Retrieve data from restful api with angularjs service $resource
我嘗試將$ resource服務與Surveygizmo api結合使用。
我的代碼:
html:
<div ng-app="Survey">
<body>
<div ng-controller="SurveyCtrl">
{{survey.data.title}}
</div>
</body>
</div>
我的腳本:
angular.module('Survey', ['ngResource']);
function SurveyCtrl($scope, $resource) {
$scope.surveygizmo = $resource('https://restapi.surveygizmo.com/v3/survey/:id',
{id: '@id'},
{get:{method:'JSONP', params: {'user:pass':'xxx@xxxx:xxxx', q:'angularjs', callback:'JSON_CALLBACK'}, isArray:true}});
$scope.survey = $scope.surveygizmo.get({id:xxxx}, function(survey) {
alert('this is ok');
}, function(err){
alert('request failed');
});
}
當我嘗試它時,警報“請求失敗”出現在我的頁面中。 頁面中沒有json結果,但我可以在Firebug網絡菜單中看到它。
我可以錯過點什么嗎?
卡拉OK
我知道這個問題很舊,但是我認為我可以提供幫助。 我實際上在SurveyGimzo工作。 實際上,我們確實支持JSONP,JSON和XML。 然而; 為了請求JSONP,您需要在url中指定此名稱。 使用示例網址,它看起來像這樣。
https://restapi.surveygizmo.com/v3/survey/:id.jsonp
現在,當您將JSON_CALLBACK作為ngResource參數的一部分用於get操作時,您將獲得一個正確包裝的對象。
我一直在研究使用SG REST API的Minnie AngularJS應用。 歡迎您來看看我的github https://github.com/sfisherGizmo/ang-app
我希望這也會對可能發生這種情況的其他所有人有所幫助。
Survey Gizmo不支持JSONP。 Survey Gizmo支持的HTTP方法是PUT,POST,DELETE,請參見http://developer.surveygizmo.com/rest-api-documentation/methods/
或者,如果他們支持,則不會在API文檔中指定。
這是當您將.get
更改為.query
時所看到的
Request URL:https://restapi.surveygizmo.com/v3/survey/xxxx
Request Method:OPTIONS
Status Code:200 OK
如果繼續使用.get,響應是
Request URL:https://restapi.surveygizmo.com/v3/survey/xxxx
Request Method:GET
Status Code:200 OK
但是響應沒有包裝在JSONP回調中。 盡管您可以在Firebug網絡控制台中看到響應,但是angular不能解包,因為它不是JSONP響應。
使用ngResource查看http://jsfiddle.net/jhsousa/aQ4XX/以獲取angularjs示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.