Angular HttpPost在REST中执行两次

时间:2016-09-06 10:48:56

标签: javascript angularjs rest

使用带有角度的$ HttpPost有两次执行它的问题。 对于您的信息,该函数在从“getActiveSpools()”函数返回promise之后执行。不知何故,“http://testserver/LitzMobileListener/LitzScannerService.svc/PostMfgoSetup/”运行两次,使数据上传两次。

我是角度和REST的新手。

非常感谢您的帮助

$scope.getActiveSpools().then(function (promise) {

        $scope.spoolSetupHeader.STATUS = status;
        $scope.spoolSetupHeader.OPTR_IN = $rootScope.globals.currentUser.employeeId;
        $scope.spoolSetupHeader.PLANT_NO = $scope.spoolSetupHeader.setupMfgo.substring(1, 2);
        $scope.spoolSetupHeader.MFGO_NO = $scope.spoolSetupHeader.setupMfgo.substring(2, 9);

        $http({
            url: "http://testserver/LitzMobileListener/LitzScannerService.svc/PostMfgoSetup/"
            , method: "POST"
            , data: JSON.stringify($scope.spoolSetupHeader)
            , dataType: "json"
            , contentType: "application/json; charset=utf-8",
        }).then(function (result) {

            $scope.returnedStatus = new ReturnedOperationStatus();
            $scope.returnedStatus.IsSuccess = result.data.IsSuccess;
            $scope.returnedStatus.Message = result.data.Message;
            $scope.spoolSetupHeader = new OrderHeader();//new SpoolSetupHeader();
            $scope.spoolSetupHeader.PO_ID = result.data.RecordKey;
            $scope.spoolSetupHeader.ReturnedInfo = result.data.ReturnedObject;

            if ($scope.returnedStatus.IsSuccess == true) {
                alertTitle = "Success";

                //upload source wire if needed
                if ($scope.swNeeded == true) {

                    $scope.uploadMfgoSetupSpools('NEW', 'Active', true);
                }
                else {
                    $scope.uploadMfgoSetupSpools('UPDATE', 'Inactive', false);
                }

                //reload saved data
                $scope.getSetupMfgo($scope.spoolSetupHeader.PO_ID, 'DETAILS_ACT', null, null);

                //init new spool details list 
                //$scope.spoolSetupHeader.setupSpools = new Array();
                //myNavigator.pushPage('partial-setupSpool.html', { animation: 'slide' })

            }
            else {
                alertTitle = "Failed";
            }

            ons.notification.alert({
                message: $scope.returnedStatus.Message,
                title: alertTitle,
                modifier: true
            });
        });
    });//end promise

1 个答案:

答案 0 :(得分:0)

我不能写评论所以我会回答。根据我的理解,您的http post请求应该运行一次。我没有机会看,但可能在

  

$ scope.getActiveSpools()。then(function(promise)

由于$ digest,

调用运行两次。您可以从https://docs.angularjs.org/api/ng/type/ $ rootScope.Scope#$ digest

查看

这可能会引导您找到正确的答案。很抱歉没有完全回答你的问题。