2017-04-04 9 views
0
<script> 
var app = angular.module("myapp",[]); 

app.controller("usercontroller",function($scope,$http){ 
$scope.insertData = function(){ 
    $http.post(
    "insert.php",  
    {'yourName':$scope.yourName, 'lstName':$scope.lstName, 
'passwrd':$scope.passwrd}).success(function(data){ 
     alert(data); 
    }); 
} 
}); 

我有這個代碼和控制檯返回一個錯誤,說「成功不是函數」我該如何解決它?使用角度和php連接到數據庫

回答

1

使用then代替success

.then(onSuccess) 
function onSuccess(data){ 
} 

而且我認爲你應該做這樣的

HTML

<input ng-model="formData.yourName"> 
<input ng-model="formData.lstName"> 
<input ng-model="formData.password"> 

在JS

 function yourcontroller($scope){ 
     var $scope.formData={}; 
      $http({ 
      method:'POST', 
      url:'insert.php', 
      data:$.param($scope.formData) 
      headers : { 'Content-Type': 'application/x-www-form-urlencoded' } 

      }) .then(onSuccess,onError) 
     function onSuccess(data){ 
     //handle success 
     } 
     function onError(reason){ 
     //handle error 
} 
     } 
0

.success折舊,使用.then。如:

.then(function(data){ 
    alert(data);// handle data 
    }, function(error) { 
     //handle error 
    });