我想通过AngularJS和Codeigniter将产品插入数据库:
我在视图welcome_message.php
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.9.0/ui-bootstrap-tpls.min.js"></script>
</head>
<body>
<div id="container" ng-app="myApp" >
<section>
<form class="form-inline" ng-controller="FormController" ng-submit="submitForm()" role="form" method="post">
<div class="form-group">
<label class="sr-only" for="Source Station">Insert Your Name</label>
<input type="text" ng-model="name" placeholder="name" class="form-control" >
</div>
<div class="form-group">
<label class="sr-only" for="Source Station">Insert Your price</label>
<input type="text" ng-model="price" placeholder="price" class="form-control" >
</div>
<button type="submit" class="btn btn-primary">Submit Record</button>
<pre>{{message}}</pre>
</form>
</section>
</div>
<script>
var App = angular.module('myApp', ['ui.bootstrap']);
function FormController($scope, $http) {
$scope.name = undefined;
$scope.price = undefined;
$scope.message = undefined;
$scope.submitForm = function ()
{
$http({
method: 'POST',
url: '<?php echo base_url(); ?>index.php/welcome/add',
headers: {'Content-Type': 'application/json'},
data: JSON.stringify({name: $scope.name,price:$scope.price})
}).success(function (data) {
console.log(data);
$scope.message = data.status;
});
}
}
</script>
</body>
welcome.php页面中的控制器代码是:
public function add()
{
// Here you will get data from angular ajax request in json format so you have to decode that json data you will get object array in $request variable
$request = json_decode(file_get_contents("php://input"), TRUE);
$name = $request->name;
$price = $request->price;
$id = $this->mproduct->AddUser($name,$price);
if($id)
{
echo $result = '{"status" : "success"}';
}else{
echo $result = '{"status" : "failure"}';
}
}
模型中的代码是:
public function AddUser($name,$price)
{
$data = array('name' =>$name,'price' =>$price);
$this->db->insert('product',$data);
$insert_id = $this->db->insert_id();
return $insert_id;
}
但是当我点击Submit Record
时,控制台会显示以下错误:
POST http://localhost/angular_autocomplate/index.php/welcome/add 500 (Internal Server Error)
并且记录剂量会添加到数据库中。
问题是什么?