如何将ajax结果分配给公共变量

时间:2019-02-03 18:23:22

标签: javascript angularjs ajax

我想将ajax结果分配给rule_id变量 变量等于未定义 如何将ajax的所有结果分配给外部变量?

var rule_id;
 $scope.addrule_info= function(){
            var cars_array = new Array();

            $scope.cars  = cars_array.toString();
            //$scope.cars  = document.getElementById('cars_id').checked.values;

            $scope.eventtype  = document.getElementById('event_typeid').value;
        //end collect data
        var url=appServices.getServerUrl()+"api/add_event_rule"
        $.ajax({
            method: "POST",
            headers: {         
                Accept: "application/json; charset=utf-8",         
                "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" ,
                Authorization:"Bearer "+appServices.getVar("token")

              }     ,
            url: url,
            async: true, 
            data: {
                name:$scope.rulename,
                  car_ids:$scope.cars,
                  event_type_id:$scope.eventtype,
                description:$scope.rule_desc
                  }
          })

            .done(function( msg ) {

                console.log(msg + "msg");
                console.log(msg.success.id + "msg");
                var rule_id = msg.success.id;

            })
         .fail(function(error){


            {
                var alertPopup = $ionicPopup.alert({
                    title: 'Error',
                    template: 'SSS',
                    button  : 'Done'
            });
    //      alertPopup.then(function(error) {
    //              console.log(error);
    //      }); 
            }
          });



        };
         $scope.addrule_info= function(){
         console.log(rule_id);
         }

我尝试了很多,但是对我来说却不起作用 谁能说出该代码出了什么问题? 预先感谢

0 个答案:

没有答案