这是我第一次构建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上,它显示“套接字挂起”错误。
1条答案
按热度按时间4bbkushb1#
PUT和DELETE方法在000webhost上的免费主机上不可用。您必须升级到Premium计划才能使用GET和POST以外的HTTP方法请参阅this post。
除了GET和POST之外的所有方法都是简单地终止于免费托管计划。在我看来,这对他们来说是非常业余的,因为他们可以很容易地发送一个响应头说“只适用于高级计划”,而不是只是直截了当地终止连接。
与此同时,开发人员正在花费大量的时间来试图找出他们完美的代码中的错误。