doubi6303 2018-07-25 14:30
浏览 392

无法加载资源:服务器响应状态为500(内部服务器错误) - Angularjs和Codeigniter

I am making a crud for my services table and I am using Angularjs and Codeigniter. But when i try to submit the data, this error happens

POST http://localhost/beauty-care-api/services/create 500 (Internal Server Error)

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">&times;</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>

This is the code from 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');
}

My 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);
}
  • 写回答

1条回答 默认 最新

  • dongtan3306 2018-07-25 15:06
    关注

    If you are using Codeigniter you must to separate the Controller functions to the Model (database) functions, so maybe it should be like this:

    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);
    }
    

    I hope this can help you.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组