Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS向REST服务提交投票申请_Javascript_Angularjs_Rest - Fatal编程技术网

Javascript AngularJS向REST服务提交投票申请

Javascript AngularJS向REST服务提交投票申请,javascript,angularjs,rest,Javascript,Angularjs,Rest,我一直在为我的网站开发一个投票功能,但我对使用AngularJS的$resource比较新,我希望用我刚才记录的投票更新现有记录。问题是它没有更新,也没有给出任何错误消息,只是在控制台日志中声明我的REST调用需要CORS preflight,这让我相信它至少正在访问后端 在开发的这一点上,我相信我可能不知道如何使用ID更新记录,因为在本例中,我没有$routeparam可供参考,但我正在尝试从具有投票功能的ng repeat更新。在屏幕上,它通过向上或向下递增来工作,但不会更新数据库,因此当我

我一直在为我的网站开发一个投票功能,但我对使用AngularJS的$resource比较新,我希望用我刚才记录的投票更新现有记录。问题是它没有更新,也没有给出任何错误消息,只是在控制台日志中声明我的REST调用需要CORS preflight,这让我相信它至少正在访问后端

在开发的这一点上,我相信我可能不知道如何使用ID更新记录,因为在本例中,我没有$routeparam可供参考,但我正在尝试从具有投票功能的ng repeat更新。在屏幕上,它通过向上或向下递增来工作,但不会更新数据库,因此当我刷新页面时,更改不会保持,因为它们从未提交

这是我的HTML:

<div ng-controller="articleVotingCtrl">
    <table class="table table-striped">
        <tr>
            <th>Votes</th>
            <th>Title</th>
            <th>Category ID</th>
        </tr>
        <tr ng-repeat="articlevote in articlevotes">
            <td>
                <div class="col-md-1 voting well">
                    <div class="votingButton" ng-click="upVote(articlevote);">
                        <i class="glyphicon glyphicon-chevron-up"></i>
                    </div>
                    <div class="badge badge-inverse">
                        <div>{{articlevote.articlevotes}}</div>
                    </div>
                    <div class="votingButton" ng-click="downVote(articlevote);">
                        <i class="glyphicon glyphicon-chevron-down"></i>
                    </div>
                </div>
            </td>
            <td>{{articlevote.articletitle}}</td>
            <td>{{articlevote.articlecategoryid}}</td>
        </tr>
    </table>
</div>
这是我的服务:

// Article Vote
pfcControllers.controller('articleVotingCtrl', ['$scope', 'pfcArticles', function($scope, pfcArticles) {
    $scope.articlevotes = pfcArticles.query();

    $scope.upVote = function(articlevote) {
        articlevote.articlevotes++;
        updateVote(articlevote.id, articlevote.articlevotes);
    };

    $scope.downVote = function(articlevote) {
        articlevote.articlevotes--;
        updateVote(articlevote.id, articlevote.articlevotes);
    };

    function updateVote(id, articlevotes) {
        pfcArticles.update({
            articleID: id
        }), articlevotes;
    }
}]);
 // All Articles
 pfcServices.factory('pfcArticles', ['$resource', function($resource) {
     return $resource('https://myrestcall.net/tables/articles', { articleID: '@id' }, {
         'update': {
             method: 'PATCH'
         }
     });
 }]);
以下是示例JSON数据:

[
  {
  "id": "66D5069C-DC67-46FC-8A51-1F15A94216D4",
  "articletitle": "artilce1",
  "articlecategoryid": 1,
  "articlesummary": "article 1 summary. ",
  "articlevotes": 1
  },
  {
  "id": "66D5069C-DC67-46FC-8A51-1F15A94216D5",
  "articletitle": "artilce2",
  "articlecategoryid": 2,
  "articlesummary": "article 2 summary. ",
  "articlevotes": 3
  }, 
  {
  "id": "66D5069C-DC67-46FC-8A51-1F15A94216D6",
  "articletitle": "artilce3",
  "articlecategoryid": 3,
  "articlesummary": "article 3 summary. ",
  "articlevotes": 0
  },   
  {
  "id": "66D5069C-DC67-46FC-8A51-1F15A94216D7",
  "articletitle": "artilce4",
  "articlecategoryid": 1,
  "articlesummary": "article 3 summary. ",
  "articlevotes": 5
  }, 
]

我通过调整服务以接受一个id并更改updatevote函数以接收作用域id并拥有一个成功的处理程序来实现它。这篇文章很有帮助:

我现在的服务如下:

pfcServices.factory('pfcArticles', ['$resource', function ($resource) {
    return $resource('https://myrestcall.net/tables/articles/:articleID', { articleID: '@id' },
    {
        'update': { method: 'PATCH' }
    }
    );
}]);
控制器设置为:

pfcControllers.controller('articleVotingCtrl', ['$scope', 'pfcArticles', function ($scope, pfcArticles) {

$scope.articlevotes = pfcArticles.query();

// Voting function
$scope.upVote = function (articlevote) {
    articlevote.articlevotes++;
    var id = articlevote.id;
    var articlevotes = articlevote.articlevotes;
    updateVote(id, articlevotes);
};
$scope.downVote = function (articlevote) {
    articlevote.articlevotes--;
    var id = articlevote.id;
    var articlevotes = articlevote.articlevotes;
    updateVote(id, articlevotes);
};

function updateVote(id, articlevotes) {
    var vote = pfcArticles.get({ articleID: id}, function () {
        vote.articlevotes = articlevotes;
        vote.$update();
    });
}
}]);

HTML没有任何调整。

您的REST函数是什么样子的?您是指我的REST数据吗?我的rest调用在上面的服务中。添加了示例rest/JSON数据