AngularJs $ http post请求将数据发送到rails控制器

时间:2016-08-21 18:10:05

标签: ruby-on-rails angularjs http post

我正在使用rails mvc,我正在使用angularjs(相当新的)用于各种javascript函数。我有一些id的数组,我想通过$ http post方法发送到我对应的rails controller的create方法。

我的服务是:

.service('teamService' , function ($http) {
  var TeamService = {};
  TeamService.saveTeam = function(player_ids) {
  $http.post('/user_teams',player_ids)
  .success(function(data,status){
    data = player_ids;
    status =  true;
});
};

相应的角度控制器功能是:

    $scope.saveTeam = function () {
  var mf = $scope.getIDs($scope.Midfielders.data);
  var df = $scope.getIDs($scope.Defenders.data);
  var fw = $scope.getIDs($scope.Forward.data);
  var gk = $scope.getIDs($scope.GoalKeeper.data);
  var player_ids = mf.concat(df,fw,gk);
  teamService.saveTeam(player_ids);
};

当我在视图中单击按钮调用控制器saveTeam函数时,它会在控制台中显示错误422无法处理的条目。我做错了什么?

1 个答案:

答案 0 :(得分:0)

根据错误消息,我相信您的发布请求会转到您的Rails控制器,但这是错误发生的地方。

Angular方面的一个调整可以帮助进行故障排除,通常是一个很好的做法,可以处理http调用的错误情况。

所以而不是

If RemoveWhitespace(tbxSubmit.Text) = RemoveWhitespace(GetCell(x, 0)) Then

也许尝试类似的事情:

DECLARE @sales table(
itemnumber int,
saledate date,
saleid int)

INSERT INTO @sales VALUES(1234, '2015-01-10', 1)
INSERT INTO @sales VALUES(1234, '2015-01-11', 2)
INSERT INTO @sales VALUES(1234, '2015-01-12', 3)
INSERT INTO @sales VALUES(1234, '2015-01-13', 4)
INSERT INTO @sales VALUES(1234, '2015-01-14', 5)
INSERT INTO @sales VALUES(1234, '2015-02-10', 6)
INSERT INTO @sales VALUES(1234, '2015-02-11', 7)
INSERT INTO @sales VALUES(1234, '2015-03-10', 8)
INSERT INTO @sales VALUES(1234, '2015-04-10', 9)
INSERT INTO @sales VALUES(1234, '2015-04-16', 10)
INSERT INTO @sales VALUES(1256, '2015-02-10', 11)
INSERT INTO @sales VALUES(1256, '2015-02-11', 12)
INSERT INTO @sales VALUES(1256, '2015-02-12', 13)
INSERT INTO @sales VALUES(1256, '2015-02-13', 14)
INSERT INTO @sales VALUES(1256, '2015-02-14', 15)
INSERT INTO @sales VALUES(1256, '2015-03-10', 16)
INSERT INTO @sales VALUES(1256, '2015-03-11', 17)
INSERT INTO @sales VALUES(1256, '2015-04-10', 18)
INSERT INTO @sales VALUES(1256, '2015-04-10', 19)
INSERT INTO @sales VALUES(1256, '2015-04-16', 20)
INSERT INTO @sales VALUES(1259, '2015-02-10', 21)
INSERT INTO @sales VALUES(1259, '2015-02-11', 22)
INSERT INTO @sales VALUES(1259, '2015-03-12', 23)
INSERT INTO @sales VALUES(1259, '2015-03-13', 24)
INSERT INTO @sales VALUES(1259, '2015-03-14', 25)
INSERT INTO @sales VALUES(1259, '2015-03-10', 26)
INSERT INTO @sales VALUES(1259, '2015-03-11', 27)
INSERT INTO @sales VALUES(1259, '2015-04-10', 28)
INSERT INTO @sales VALUES(1259, '2015-04-10', 29)
INSERT INTO @sales VALUES(1259, '2015-04-16', 30)
INSERT INTO @sales VALUES(1255, '2015-03-10', 31)
INSERT INTO @sales VALUES(1255, '2015-03-11', 32)
INSERT INTO @sales VALUES(1255, '2015-03-12', 33)
INSERT INTO @sales VALUES(1255, '2015-03-13', 34)
INSERT INTO @sales VALUES(1255, '2015-03-14', 35)
INSERT INTO @sales VALUES(1255, '2015-03-10', 36)
INSERT INTO @sales VALUES(1255, '2015-03-11', 37)
INSERT INTO @sales VALUES(1255, '2015-03-10', 38)
INSERT INTO @sales VALUES(1255, '2015-04-10', 39)
INSERT INTO @sales VALUES(1255, '2015-04-16', 40)

SELECT s.itemnumber, 
right(convert(varchar, fd.firstsaledate, 103), 7) AS firstsale, 
COUNT(*) as firstmonthsales FROM @sales s
INNER JOIN 
(SELECT itemnumber, Min(saledate) as firstsaledate FROM @sales 
GROUP BY itemnumber) as fd
ON MONTH(s.saledate) = MONTH(fd.firstsaledate) 
AND YEAR(s.saledate) = YEAR(fd.firstsaledate)
AND s.itemnumber = fd.itemnumber
GROUP BY s.itemnumber, fd.firstsaledate

.then()的第一个函数是处理http调用成功结果的函数,只有在http调用发生错误时才会调用第二个函数。

Angular的$ http:https://docs.angularjs.org/api/ng/service/ $ http#

的其他信息

希望这有帮助, 崖