【问题标题】:Restangular does not workRestangular不起作用
【发布时间】:2014-01-26 16:41:43
【问题描述】:

Restangular 缺乏文档,这让我很生气。 我正在尝试使用 REST 端点。我使用 Django-Tastypie 作为后端提供程序。

var baseQuestions = Restangular.all('questions');
baseQuestions.one($routeParams.questionId).get().then(function(question) {
    console.log(question);
});

在我的 AngularJS 应用程序配置函数中,我确保我以正确的方式设置了 Restangular。

RestangularProvider.setBaseUrl('/api/v1');
RestangularProvider.setRequestSuffix('?format=json');
RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
    var newResponse;
    if (operation === 'getList') {
        newResponse = response.objects;
        newResponse.metadata = response.meta;
    } else {
        newResponse = response.data;
    }
    return newResponse;
});

但打印的控制台值显示问题未定义,显然我不希望这样,我需要实际数据。

REST 端点在浏览器中运行良好。

谁能告诉我我做错了什么?

【问题讨论】:

  • 你能console.log(response, operation)吗?

标签: javascript angularjs rest tastypie restangular


【解决方案1】:

当使用 Restangular 调用一个独特的元素时,我建议这样做:

Restangular.one('questions', $routeParams.questionId).get().then(
    function(question) {
        console.log(question);
    });

如果有帮助请告诉我。

【讨论】:

  • 这可能听起来很疯狂,但它今天才开始工作......我不确定我做了什么改变,我会查看 Git 提交。感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多