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

时间:2018-07-25 14:30:05

标签: php angularjs ajax 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);
}

1 个答案:

答案 0 :(得分:0)

如果使用Codeigniter,则必须将Controller函数与Model(数据库)函数分开,所以也许应该像这样:

ServiceController.php

Function execution took 10 ms, finished with status: 'ok'

ServiceModel.php

exports.generateThumbnail = functions.storage.object().onFinalize((object) => {
  const fileBucket = object.bucket; // The Storage bucket that contains the file.
  const filePath = object.name; // File path in the bucket.
  const contentType = object.contentType; // File content type.

  // Exit if this is triggered on a file that is not an image.
  if (!contentType.startsWith('image/')) {
    console.log('This is not an image.');
    return null;
  }

  // Get the file name.
  const fileName = path.basename(filePath);
  // Exit if the image is already a thumbnail.
  if (fileName.startsWith('thumb_')) {
    console.log('Already a Thumbnail.');
    return null;
  }

  // Download file from bucket.
  const bucket = gcs.bucket(fileBucket);

  const metadata = {
    contentType: contentType,
  };
  // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
  const thumbFileName = `thumb_${fileName}`;
  const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
  // Create write stream for uploading thumbnail
  const thumbnailUploadStream = bucket.file(thumbFilePath).createWriteStream({metadata});

  // Create Sharp pipeline for resizing the image and use pipe to read from bucket read stream
  const pipeline = sharp();
  pipeline
    .resize(THUMB_MAX_WIDTH, THUMB_MAX_HEIGHT)
    .max()
    .pipe(thumbnailUploadStream);

  bucket.file(filePath).createReadStream().pipe(pipeline);

  const streamAsPromise = new Promise((resolve, reject) =>
    thumbnailUploadStream.on('finish', resolve).on('error', reject));

  return streamAsPromise.then(() => {
    console.log('Thumbnail created successfully');
    return null;
  });

希望这对您有所帮助。