# How to validate subsequent AJAX requests?

**URL:** <https://discourse.slimframework.com/t/how-to-validate-subsequent-ajax-requests/2165>\
**Category:** Questions\
**Created:** [January 16, 2018, 11:12am UTC](https://discourse.slimframework.com/t/how-to-validate-subsequent-ajax-requests/2165 "2018-01-16T11:12:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emilkitua](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/emilkitua/32/602_2.png) [@emilkitua](https://discourse.slimframework.com/u/emilkitua)\
**Post date:** [January 16, 2018, 11:12am UTC](https://discourse.slimframework.com/t/how-to-validate-subsequent-ajax-requests/2165/1 "2018-01-16T11:12:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 16, 2018, 12:51pm UTC](https://discourse.slimframework.com/t/how-to-validate-subsequent-ajax-requests/2165/2 "2018-01-16T12:51:12Z")

</div>

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`:

```php
$configuration = [
    'settings' => [
        'displayErrorDetails' => true,
    ],
];

```

[https://www.slimframework.com/docs/handlers/error.html](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/](https://akrabat.com/logging-errors-in-slim-3/)

---

<div class="post-metadata">

**Author:** ![rodrigo](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/rodrigo/32/603_2.png) [@rodrigo](https://discourse.slimframework.com/u/rodrigo)\
**Post date:** [January 17, 2018, 10:35am UTC](https://discourse.slimframework.com/t/how-to-validate-subsequent-ajax-requests/2165/3 "2018-01-17T10:35:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![emilkitua](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/emilkitua/32/602_2.png) [@emilkitua](https://discourse.slimframework.com/u/emilkitua)\
**Post date:** [April 19, 2018, 3:43am UTC](https://discourse.slimframework.com/t/how-to-validate-subsequent-ajax-requests/2165/4 "2018-04-19T03:43:27Z")

</div>

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