How to validate subsequent AJAX requests?


#1

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(r_id);
// console.log(csrfValue);
var datastring = {“id” : r_id, “csrf_name” : csrfName, “csrf_value” : csrfValue};
$.ajax({
type:‘POST’,
url:’{{ base_url() }}/reports/districts’,
data:datastring,
success:function(data){
console.log(data);
item = [];
for (var i=0; i < data.length; i++)
{
$("#district").html(data);
var district = myTrim(’’ + data[i].d_name + ‘’);
item.push(district);
//$("#district").appendAdd(district);
console.log(district);
}
$("#district").append(item);
// 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);
                        
                        $.ajax({
                            type:'POST',
                            url:'{{ base_url() }}/reports/hospital',
                            data:datastring,
                            success:function(data){
                                console.log(data);

                                item = [];
                                for (var i=0; i < data.length; i++)
                                {
                                    $("#hospital").html(data);
                                    var hospital = myTrim('<option value="' + data[i].h_id + '">' + data[i].h_name + '</option>');
                                    item.push(hospital);
                                    //$("#hospital").appendAdd(hospital);
                                    console.log(hospital);
                                }
                                $("#hospital").append(item);
                                // 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?


#2

Hi!

“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,
    ],
];

https://www.slimframework.com/docs/handlers/error.html

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

https://akrabat.com/logging-errors-in-slim-3/


#3

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