I know there are tons of similar questions but none seem to help so far.
So, I have AJAX POST within a QR code scanner function. The POST will send "string A" to CI4 controller and return back with "string B" to the view.
View (scan-attendance.php
):
qrcode.callback = (res) => {
if (res) {
outputData.innerText = res;
console.log(res);
$.ajax({
url: '<?php echo base_url("pengran/absen") ?>',
type: 'post',
dataType: 'json',
data: {
res: res
},
success: function(data) {
console.log(data);
}
});
}
};
Routes (Routes.php
):
$routes->group('pengran', function ($routes) {
$routes->post('absen', 'OfficialController::verify_attendant');
});
Controller ('OfficialController')
class OfficialController extends BaseController
{
public function verify_attendant()
{
d("#1");
if ($this->request->isAJAX()) {
$query = service('request')->getPost('res');
d($this->request->getPost('res'));
}
d("#2");
// doing some CRUD and getting some data from database... let's say, $result = "String B"
$result = "String B";
return view('pengran/scan-attendance', $result);
}
}
I used to get 500 internal server error, but after fiddling with the code, now the error is gone but it seems verify_attendant()
is never called. I never get "#1" in the browser console, let alone the CRUD and returning with "String B" back to the view.
Please help
question from:
https://stackoverflow.com/questions/65902319/codeigniter-4-ajax-post-to-controller-method-not-working 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…