1

Hi i am new to angular js, i am trying to create CRUD operations in angular js. But i am stuck in POST error which tells 500 internal server error when i am posting from form.

My controller code:

 public function index_post(){
 $bookId = $this->API_model->insert_entery($_POST);
if(! is_null($bookId)){
    $this->response(array("response" => $bookId),200);
 }

else{
        $this->response(array("error" =>"coudn't post"),400);
    }
    echo $this->db->last_query();
}

My model: API_model

     public function insert_entery($options=array())
        {
      if(isset($options["book_name"])){
        $this-> db -> set("book_name",$options["book_name"]);
    }

    if(isset($options["book_price"])){
        $this-> db -> set("book_price",$options["book_price"]);
    }

    if(isset($options["book_author"])){
        $this-> db -> set("book_author",$options["book_author"]);
    }
    return $this-> db->insert('books');
}

My view:

 <div ng-app="myApp" ng-controller="cntrl">
 <form>
   Book name <input type="text" ng-model="book_name" name="book_name"    ng-disabled="obj.idisable">
 Book price: <input type="text" ng-model="book_price" name="book_price">
 Book author: <input type="text" ng-model="book_author" name="book_author">
 <input type="button"  ng-click="insertdata()" >
 {{msg}}

 </form>

 </div>
 <script>
var app=angular.module('myApp',[]);
app.controller('cntrl', function($scope,$http){
$scope.obj={'idisable':false};
$scope.btnName="Insert";

$scope.insertdata=function(){
$http.post("/books",{'book_name':$scope.book_name,   'book_price':$scope.book_price
        ,'book_author':$scope.book_author
})
    .success(function(){
        $scope.msg="Data Inserted";


    })

}
});
</script>
Rakesh
  • 11
  • 3
  • Can you please set content type? at right before your $http.post call? – Jigar7521 Oct 17 '16 at 08:27
  • and if you add a .then(function(response) { }, function(error) { }) event handler to the $http.post() call instead of .success() you should be able to see the error message attached to the 500 error response... – Neil Hibbert Oct 17 '16 at 08:34
  • thanks .. angular js error is : angular.min.js:93 XHR finished loading: POST – Rakesh Oct 17 '16 at 08:42

0 Answers0