【问题标题】:Failed to load resource: the server responded with a status of 500 (Internal Server Error) - Angularjs and Codeigniter加载资源失败:服务器响应状态为 500(内部服务器错误) - Angularjs 和 Codeigniter
【发布时间】:2018-07-25 14:30:05
【问题描述】:

我正在为我的服务表做一个粗略的处理,我正在使用 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);
}

【问题讨论】:

  • 500 内部服务器错误意味着您的 php-script 中有错误。查看您的日志文件以了解它是什么。

标签: php angularjs ajax codeigniter


【解决方案1】:

如果您使用 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);
}

希望对你有帮助。

【讨论】:

  • 感谢您的帮助,但我仍然遇到同样的错误:(
  • aww 如果尝试用这个替换函数“create”的所有内容: *public function create() { echo json_encode(['status'=>1]); } * 仍然失败?
  • 是的。仍然失败:(
  • 我还尝试在 alert() 中显示数据以检查我是否正在获取数据,并在 alert() 中显示我的数据输出。我不知道为什么我不能将数据传递给 codeigniter 中的 create() 函数。
猜你喜欢
  • 2018-01-22
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 2014-11-24
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多