Angular JS双重请求

时间:2013-07-22 19:46:18

标签: javascript http angularjs

以下是设置示例:

var editor = angular.module("editor",['ngResource']);
editor.factory("Provider",function($resource){
    return $resource('/api/test/1').get();
})

function ctrl($scope,Provider){
    $scope.sections = Provider; 
}

我的问题是在firebug中我发现有两个GET请求被发送:一个在/ api / test / 1,一个在/ api / test / 1 /。

任何可能导致此问题的想法?

2 个答案:

答案 0 :(得分:1)

为了完成,我相信$http会在您的文件夹结构中“短路”,只需抓住您指向它的资源并且可以在项目中访问,而$resource是适合与外部源通信,通常是外部RESTful api。在我实现它的应用程序中,我经常定义一个变量,我的各种$resource定义可以访问它,正如您在我定义的变量API

中看到的那样

在这种情况下,您还会将1作为某种身份传递。

var API = 'http://localhost\\:#####/api/';

editor.factory('Provider', function ($resource) {
    return $resource(API + 'test/:id', { id: '@id' }, { });
});

然后实施

function ctrl($scope, Provider){
    var myVarToPass = 1;

    Provider.get({ id: myVarToPass }, function (result) {
        //single call here as soon as controller loads
    });
}

答案 1 :(得分:1)

我发现只需在/#之后添加/1即可解决问题。

变化:

return $resource('/api/test/1')....

为:

return $resource('/api/test/1/#')....