首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >angular.js $http.post不工作-无错误

angular.js $http.post不工作-无错误
EN

Stack Overflow用户
提问于 2013-04-30 19:58:33
回答 8查看 37.6K关注 0票数 17

我正在尝试使用$http提交一个新的评论。你可能已经从标题中猜到了:它不工作。我试过海岸版和长版,都失败了。无控制台错误。

这是我的代码:

$scope.comment = {};
$scope.comment["comment"] = message; // message defined somewhere else

$http.post('/api/items/'+$scope.item.id+'/comments', $scope.comment)
    .success(function(data, status, headers, config) {
         // this isn't happening:
         console.debug("saved comment", $scope.comment);
    })
    .error(function(data, status, headers, config) {
         // this isn't happening:
         console.debug("saved comment", $scope.comment);
    })
}

有没有人知道该怎么做?谢谢!

更新:

我现在将它作为一个Jquery ajax调用,它工作得很好。不过,如果能让它与angular一起工作就更好了。然而,这是我的JQuery代码:

            var request = $.ajax({
                url: '/api/items/'+$scope.item.id+'/comments',
                type: "POST",
                data: JSON.stringify($scope.comment),
                contentType: 'application/json; charset=utf-8',
                dataType: "json"
            });

            request.done(function(msg) {
                console.debug("saved comment", $scope.comment);
            });

            request.fail(function(jqXHR, textStatus) {
                alert( "Request failed: " + textStatus );
            });

如果任何人有任何想法如何角度这请告诉我,将是很好的做它的正确方式…

EN

回答 8

Stack Overflow用户

发布于 2013-10-09 06:20:49

在发出请求之前,您是否尝试过专门设置Content-Type?

我也有同样的问题,设置Content-Type可以解决这个问题。

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
票数 7
EN

Stack Overflow用户

发布于 2013-07-18 13:25:37

也许这会对你有帮助。我遇到了类似的get call问题:AngularJS $http not firing get call

我用solution解决了这个问题,在这里找到了https://github.com/angular/angular.js/issues/2794#issuecomment-18807158,所以我用$scope.$apply包装了我的调用函数。

$scope.$apply(function() {
                console.log('Klic na ID ' + data.context.id);
                $scope.commonController.getData('orgunit/' + data.context.id + '?jsonDepth=3')
                    .success(function(workpositionData,status,headers,config) {
                        console.log('Klic na ID ' + data.context.id + ' OK');
                        $scope.workPositions = workpositionData.workPositions;
                    }).error(function(data,status,headers,config) {
                        commonController.error('Pri branju delovnih mest je prišlo do napake: '+data.description);
                    });
            });
票数 5
EN

Stack Overflow用户

发布于 2013-05-01 01:44:47

我想,你只是忘记了一些残酷的")“尝试

$http.post('/api/ideas/'+$scope.item.id+'/comments', $scope.comment)
   .success(function(data, status, headers, config) {
     // this isn't happening:
     console.debug("saved comment", $scope.comment);
   })  //<-- damn ")"
   .error(function(data, status, headers, config) {
     // this isn't happening:
     console.debug("saved comment", $scope.comment);
   }) //<--- damn ")"
 }

我还没有测试它,但我敢打赌,这就是错误

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16299362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档