REST API 在本地主机上工作,但在虚拟主机上不工作

REST API work on localhost but not on web hosting

所以这是我第一次构建 REST API,当我使用 localhost 测试 API 时,它的工作就像一个魅力但是当我在主机上托管 REST API 项目时突然它不起作用。 GET 和 POST 函数仍然有效,但 DELETE 和 PUT 函数无效。

我正在使用 000webhost 免费托管作为我的测试平台。

我在控制器上的删除功能:

public function delete($id = null)
{
    $model = new BarangModel();
    $data = $model->where('id_barang', $id)->delete($id);
    if ($data) {
        $model->delete($id);
        $response = [
            'status'   => 200,
            'error'    => null,
            'messages' => [
                'success' => 'Barang berhasil dihapus!'
            ]
        ];
        return $this->respondDeleted($response);
    } else {
        return $this->failNotFound('No employee found');
    }
}

我的型号代码:

class BarangModel extends Model
{
    protected $DBGroup              = 'default';
    protected $table                = 'tb_barang';
    protected $primaryKey           = 'id_barang';
    protected $useAutoIncrement     = true;
    protected $insertID             = 0;
    protected $returnType           = 'array';
    protected $useSoftDeletes       = false;
    protected $protectFields        = true;
    protected $allowedFields        = ['kode', 'nama', 'merk', 'tahun', 'jumlah', "ruangan", 'penguasaan', 'keterangan'];

    // Dates
    protected $useTimestamps        = false;
    protected $dateFormat           = 'datetime';
    protected $createdField         = 'created_at';
    protected $updatedField         = 'updated_at';
    protected $deletedField         = 'deleted_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks       = true;
    protected $beforeInsert         = [];
    protected $afterInsert          = [];
    protected $beforeUpdate         = [];
    protected $afterUpdate          = [];
    protected $beforeFind           = [];
    protected $afterFind            = [];
    protected $beforeDelete         = [];
    protected $afterDelete          = [];
}

控制台中的错误代码:

Error: Network Error
    at t.exports (createError.js:16:15)
    at XMLHttpRequest.b.onerror (xhr.js:117:14)
ae @ vue.runtime.esm.js:1897
re @ vue.runtime.esm.js:1888
ne @ vue.runtime.esm.js:1848
(anonymous) @ vue.runtime.esm.js:1865
Promise.catch (async)
ie @ vue.runtime.esm.js:1865
n @ vue.runtime.esm.js:2188
ie @ vue.runtime.esm.js:1863
In.t.$emit @ vue.runtime.esm.js:3903
click @ VBtn.ts:163
ie @ vue.runtime.esm.js:1863
n @ vue.runtime.esm.js:2188
Qr.a._wrapper @ vue.runtime.esm.js:6961

xhr.js:210          
DELETE https://xxx.000webhostapp.com/api/index.php/barang/106 net::ERR_HTTP2_PROTOCOL_ERROR
(anonymous) @ xhr.js:210
t.exports @ xhr.js:15
t.exports @ dispatchRequest.js:58
u.request @ Axios.js:108
i.forEach.u.<computed> @ Axios.js:129
(anonymous) @ bind.js:9
(anonymous) @ Barang.vue:292
u @ runtime.js:63
(anonymous) @ runtime.js:294
(anonymous) @ runtime.js:119
i @ asyncToGenerator.js:3
s @ asyncToGenerator.js:25
(anonymous) @ asyncToGenerator.js:32
(anonymous) @ asyncToGenerator.js:21
t @ Barang.vue:291
ie @ vue.runtime.esm.js:1863
n @ vue.runtime.esm.js:2188
ie @ vue.runtime.esm.js:1863
In.t.$emit @ vue.runtime.esm.js:3903
click @ VBtn.ts:163
ie @ vue.runtime.esm.js:1863
n @ vue.runtime.esm.js:2188
Qr.a._wrapper @ vue.runtime.esm.js:6961

在 postman 上,显示 'Socket hang up' 错误。

PUT 和 DELETE 方法不适用于 000webhost. You have to upgrade to the Premium plan to use HTTP methods other than GET and POST See this post 上的免费托管。

除 GET 和 POST 之外的所有方法都简单地终止了免费托管计划。在我看来,这对他们来说非常业余,因为他们可以轻松地发送回复 header 说“仅适用于高级计划”,而不是直接终止连接。

与此同时,开发人员正在浪费大量时间试图找出他们完美代码的问题所在。