angular js发布数据

时间:2017-03-09 05:28:58

标签: javascript php

错误

  

angular.js:14362 TypeError:$ http.post(...)。success不是函数       在ChildScope。$ scope.insertData(myjs.js:6)       at fn(eval at compile(angular.js:15197),:4:150)       在回调(angular.js:26808)       在ChildScope。$ eval(angular.js:18017)       在ChildScope。$ apply(angular.js:18117)       在HTMLButtonElement。 (angular.js:26813)       在defaultHandlerWrapper(angular.js:3617)       在HTMLButtonElement.eventHandler(angular.js:3605)

html页面:

    <body ng-app="mymod">      
       <div class="container" ng-controller="mycontroller">
       <div class="main-head">
       <img src="img/main.jpg" class="img img-thumbnail" alt="main image">

           </div>
       <div class="row">
    <div class="col-sm-5">

        <form>



  <div class="form-group">
    <label for="firstname">First name</label>
    <input type="text" class="form-control" id="firstname"  ng-model="firstname" name="firstname">
   </div>


  <div class="form-group">
    <label for="lastname">Last Name</label>
    <input type="text" class="form-control" id="lastname" ng-model="lastname" name="lastname">
  </div>



    <div class="form-group">
    <label for="district">District</label>
    <input type="text" class="form-control" id="district" ng-model="district" name="district">
  </div>


  <div class="form-group">
    <label for="dateofborth">Date Of Birth</label>
    <input type="text" class="form-control" id="datepicker"  ng-model="dob" name="dob">
  </div> 


        <button type="submit"   ng-click="insertData()" class="btn btn-info btn-sm"> Submit</button>     

        </form>                                   

           </div>


                <div class="col-sm-6 col-sm-offset-1 right">                                          <table class="table table-condensed">

                    <thead>
                       <tr>
                        <th> First Name</th>
                        <th> Last Name</th>
                        <th> District</th>
                        <th> Date Of Birth</th>
                        </tr>
                    </thead>
                    <tbody>
                    <tr ng-repeat="student in data">
                        <td>{{ student.firstname }}</td>
                        <td>{{ student.lastname }}</td>
                        <td>{{ student.district }}</td>
                        <td>{{ student.dob }}</td>

                    </tr>
                    </tbody>


                </table> 

Insert.php页面

include('connectdb.php');


$data = json_decode(file_get_contents("php://input"));

$firstname = $dbhandle->real_escape_string($data->firstname);
$lastname = $dbhandle->real_escape_string($data->lastname);
$district = $dbhandle->real_escape_string($data->district);
$dob = $dbhandle->real_escape_string($data->dob);



$query  = "INSERT INTO students VALUES($id,'".$firstname."','".$lastname."','".$district."','".$dob."')";


$dbhandle->query($query);

js文件

var app = angular.module("mymod",[]);
app.controller("mycontroller",function($scope,$http){

$scope.insertData = function(){
    $http.post("insert.php",{'firstname':$scope.firstname,'lastname':$scope.lastname,'district':$scope.district,'dob':$scope.dob})
    .success(function(){    
        $scope.msg = "Data Submitted";

    })


}   



});

请帮帮我,我找不到错误:( 我想发送数据我的数据库

2 个答案:

答案 0 :(得分:0)

试试这个:

$scope.insertData = function($http){
    $http.post("insert.php",{'firstname':$scope.firstname,'lastname':$scope.lastname,'district':$scope.district,'dob':$scope.dob})
    .success(function(){    
        $scope.msg = "Data Submitted";

    })
}

答案 1 :(得分:0)

试试这个$ http处理程序 -

$http.post('/someUrl', data, config).then(function(res){
     // The Code Here which runs on success callback
  }, function(err){
      // The Code Here which runs on Error callback
 });

返回HttpPromise。