0

我尝试将 $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

4

2 回答 2

1

I know this question is old, but I thought I might be able to help. I actually work at SurveyGimzo. We actually do support JSONP, JSON and XML. However; in order to request JSONP you need to specify this in the url. Using your example URL, it would look like this.

https://restapi.surveygizmo.com/v3/survey/:id.jsonp

Now when you request JSON_CALLBACK as part of the your ngResource parameters for your get action, you will get a properly wrapped object back.

I have been tinkering around with a minnie AngularJS app using SG REST api. You are welcome to take a look at my github https://github.com/sfisherGizmo/ang-app

I hope this helps anyone else who may happen across this as well.

于 2015-01-13T19:51:36.320 回答
0

调查 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 响应。

检查http://jsfiddle.net/jhsousa/aQ4XX/使用 ngResource 的 angularjs 示例

于 2013-06-11T05:58:46.537 回答