How to validate subsequent AJAX requests?

i am using slim/csrf for tokens, now i am trying to make two AJAX request, the first one works perfectly, but the second one doesnt.

i have tried setting the constructor parameter $persistentTokenMode to true but still it doesnt work. currently i am facing error 500 (Internal Server Error).

here is my ajax code:

` function getDistricts(){
var r_id = document.getElementById(“region”).value;
var csrfName = document.getElementById(‘csrf_name’).value;
var csrfValue = document.getElementById(‘csrf_value’).value;
// console.log(csrfValue);
var datastring = {“id” : r_id, “csrf_name” : csrfName, “csrf_value” : csrfValue};
url:’{{ base_url() }}/reports/districts’,
item = [];
for (var i=0; i < data.length; i++)
var district = myTrim(’’ + data[i].d_name + ‘’);
// console.log(district);

                    function getHospital(){
                        var d_id = document.getElementById("district").value;
                        var csrfName = document.getElementById('csrf_name').value;
                        var csrfValue = document.getElementById('csrf_value').value;
                        var datastring = {"id" : d_id, "csrf_name" : csrfName, "csrf_value" : csrfValue};
                        // console.log(datastring);
                            url:'{{ base_url() }}/reports/hospital',

                                item = [];
                                for (var i=0; i < data.length; i++)
                                    var hospital = myTrim('<option value="' + data[i].h_id + '">' + data[i].h_name + '</option>');
                                // console.log(hospital);

my routes code:
$this->post('/districts',ReportController::class . ':districts'); $this->post('/hospital',ReportController::class . ':hospitals');

my controller:
`public function districts(ServerRequestInterface $request, ResponseInterface $response) {
$id = $request->getParam(‘id’);
$districts = District::where(‘r_id’, $id)->get();

	return $response->withJson($districts, 200);


public function hospitals(ServerRequestInterface $request, ResponseInterface $response) {

    $id = $request->getParam('id');
    $hospitals = Hospital::where('d_id', $id)->get();

    return $response->withJson($hospitals, 200);

on the first one get districts it is working, but on the second one get hospitals it is returning ERROR: 500 (Internal Server Error)

What may be the problem?


“Code 500 - Internal Server Error” is a very general message. This is a general message from the server that an error has occurred, which is almost certainly due to the configuration of the server or the incorrect execution of a server script.

The default error handler can also include detailed error diagnostic information. To enable this you need to set the displayErrorDetails setting to true:

$configuration = [
    'settings' => [
        'displayErrorDetails' => true,

To see where the problem is, you should log all errors in a logfile. Here you can find instructions:

there was a time I am struggle on that problem. But now I figure out how. Do you still need some help ? Im asking because you are not replying on @odan. I am filipino, my english is bad and sorry for that. thanks

sorry for the late reply. i did figure it out. thank you @odan and @rodrigo