Angular js无法使用$ http.post发布数据

时间:2014-05-14 19:27:01

标签: django angularjs http-post

我有一个有角度的应用程序,我试图将一些元素发布到数据库中。在这里,我使用模态,我检查一些课程,以便使用$http.post发布选中的课程这是发布数据的功能:

$scope.ok = function () {
              //$scope.subcategory.subcategory2 = [];
                for(var i = 0; i < $scope.selectedCourses.length; i++){
                    debugger
                  //$scope.subcategory.subcategory2.push({course: $scope.selectedCourses[i], term:"--",credit:"--",grade:"--"});
                  var variablesToSend = {
                                        "student": 2773951,
                                        "course_name": $scope.selectedCourses[i].course.course_name,
                                        "title": $scope.selectedCourses[i].course.title,
                                        "credit": 3,
                                        }
                    $http.post('/api/studentplannedcredit/', variablesToSend).then(function(response){
                        console.log(response);
                        //alert('post added');
                        //document.location.reload(true);

                    }, function(response){
                        console.log(response);
                        alert('Unable to add Course');
                    });             
                }
                $modalInstance.close();
            };

我得到的错误是,当我调用该函数时,它给出了以下错误:

Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.2.8/$rootScope/inprog?p0=%24digest
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:78:12
    at beginPhase (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:12262:15)
    at Scope.$get.Scope.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:12048:11)
    at Scope.$delegate.__proto__.$apply (<anonymous>:855:30)
    at HTMLDivElement.<anonymous> (http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.min.js:8:20385)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:2612:10
    at forEach (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:309:20)
    at HTMLDivElement.eventHandler (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:2611:5)
    at http://127.0.0.1:8000/static/js/app.js:161:7
    at deferred.promise.then.wrappedErrback (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js:10952:78) angular.js:9413
(anonymous function) angular.js:9413
$get angular.js:6832
$get.Scope.$apply angular.js:12051
$delegate.__proto__.$apply VM437152:855
(anonymous function) ui-bootstrap-tpls-0.10.0.min.js:8
(anonymous function) angular.js:2612
forEach angular.js:309
eventHandler angular.js:2611
(anonymous function) app.js:161
deferred.promise.then.wrappedErrback angular.js:10952
deferred.promise.then.wrappedErrback angular.js:10952
(anonymous function) angular.js:11078
$get.Scope.$eval angular.js:11949
$get.Scope.$digest angular.js:11775
$delegate.__proto__.$digest VM437152:844
$get.Scope.$apply angular.js:12055
$delegate.__proto__.$apply VM437152:855
done angular.js:7837
completeRequest angular.js:8020
xhr.onreadystatechange angular.js:7976

2 个答案:

答案 0 :(得分:1)

我不确定它是在你的例子中的某个地方,还是在其他地方你做错了。 下面是一个角度为POST的工作示例。

var config = {
    method: 'POST',
    url: '/api/studentplannedcredit/',
    data: variablesToSend 
};


$http(config)
.success(function(data, status, headers, config) {
    console.log(data);
})
.error(function(data, status, headers, config) {
    console.log(data);
});

PS:不要为每个循环发布POST ...这将严重破坏API ..先收集数据然后再发送1个POST。

答案 1 :(得分:0)

我假设,$ modalInstance是这里的问题。

尝试不用

运行
$modalInstance.close();