angular.js $ http.post不工作​​ - 没有错误

Din*_*ine 17 angularjs

我正在尝试使用$ 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);
    })
}
Run Code Online (Sandbox Code Playgroud)

任何人都知道如何使这项工作?谢谢!

更新:

我现在正在做一个Jquery ajax调用,它运行正常.尽管如此,让它与角度一起工作会很好.这是我的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 );
            });
Run Code Online (Sandbox Code Playgroud)

如果有人有任何想法如何对此进行角度化请告诉我,以正确的方式做到这一点会很好....

dad*_*ade 7

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

我有同样的问题,并设置内容类型修复它.

$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
Run Code Online (Sandbox Code Playgroud)


Sob*_*bis 5

也许这会对你有所帮助.我有类似的问题与获取调用:AngularJS $ http没有触发get调用

我用解决方案解决了它,在这里找到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);
                    });
            });
Run Code Online (Sandbox Code Playgroud)


kfi*_*fis 2

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

$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 ")"
 }
Run Code Online (Sandbox Code Playgroud)

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