我正在为我的服务表制作crud,我正在使用Angularjs和Codeigniter。但是当我尝试提交数据时,会发生这个错误
POSThttp://localhost/beauty-care-api/services/create500(内部服务器错误(
services.html
<!-- Service Modal -->
<div class="modal fade" id="modal-id">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">New Service</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="product_name">Service Name:</label>
<input type="text" id="service_name" class="form-control" ng-model="service.service_name">
</div>
<div class="form-group">
<label for="price">Price:</label>
<input type="number" id="price" class="form-control" min="0" ng-model="service.price">
</div>
<div class="form-group">
<label for="available">Available:</label>
<select id="available" class="form-control" ng-model="service.availability">
<option ng-repeat="availability in availabilities" ng-value="availability">
{{ availability }}
</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button ng-if="!edit" type="button" class="btn btn-primary" ng-click="saveService(service)">Save</button>
<button ng-if="edit" type="button" class="btn btn-primary" ng-click="updateProduct(service)">Save changes</button>
</div>
</div>
</div>
</div>
这是ServiceController.js 的代码
$scope.service = {
service_name: '',
price: 0,
availability: 'active'
};
$scope.saveService = function(service){
httpService.post(`${urlService.apiURL}services/create`, angular.toJson(service))
.then((res) => {
listServices();
});
$('#modal-id').modal('toggle');
}
我的httpService.js
var httpService = angular.module('httpService', [])
.service('httpService', function($http) {
this.get = function (url) {
return $http.get(url);
}
this.post = function (url, data) {
return jQuery.ajax({
type: 'POST',
url: url,
data: { data: data }
});
} });
ServiceController.php
public function create(){
$data = array(
'service_name' => $this->input->post('service_name'),
'status' => $this->input->post('available')
);
$this->db->set($data)
->insert('tbl_services');
$res['status'] = '1';
echo json_encode($res);
}
如果您使用Codeigniter,您必须将Controller函数与Model(数据库(函数分离,因此可能应该是这样的:
ServiceController.php
public function __construct() //Controller constructor
{
parent::__construct();
$this->load->helper('url');
$this->load->model('ServiceModel'); //load your model
}
public function create()
{
$data = array (
'service_name' => $this->input->post('service_name'),
'status' => $this->input->post('available')
);
$res['status'] = '0'; // default status response
$model = new ServiceModel(); //create an instance of your model
$response = $model->insertData($data); // insert the data
if($response) //if the insert was successful(true) set "status" = 1
{
$res['status'] = '1';
}
echo json_encode($res);
}
ServiceModel.php
function __construct() //Model constructor
{
parent::__construct();
$this->db = $this->load->database('default',TRUE);
}
public function insertData($mdata)
{
return $this->db->insert('tbl_services', $mdata);
}
我希望这能帮助你。