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?