簡體   English   中英

加載資源失敗:服務器響應狀態為500(內部服務器錯誤)-Angularjs和Codeigniter

[英]Failed to load resource: the server responded with a status of 500 (Internal Server Error) - Angularjs and Codeigniter

我正在為我的服務表做好准備,並且正在使用Angularjs和Codeigniter。 但是當我嘗試提交數據時,會發生此錯誤

POST http:// localhost / beauty-care-api / services / create 500(內部服務器錯誤)

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>

這是來自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);
}

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM