So this is my first time building a REST API and when I test the API using localhost it's working like a charm but when I host the REST API project on hosting suddenly it does not work. GET and POST functions still working but DELETE and PUT functions aren't.
I am using 000webhost free hosting as my test platform.
my delete function on Controller:
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');
}
}
My model code:
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 code in console:
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
On postman, it shows a 'Socket hang up' error.