I have worked with Slim Framework ;)
Look, this is how I have managed to make Ajax work with Slim framework.
First, you have to declare the routes both get and post (when I first tried declaring post only, it didn't work):
GET:
$app->get('/your/url',$permission(),function() use ($app){
//here you can check whether it is an ajax call
})->name('your.url');
POST:
$app->post('/your/url',$permission(),function() use ($app){
//get the data:
$request = $app->request;/*getting post data*/
$var1 = $request->post('var1');
$var2 = $request->post('var2');
//echo '| For log and testing purposes you can echo out the gotten values. Var1: '.$var1.'. Var2: '.$var2;
//DO YOUR STUFF here, for example, database processing with Eloquent ORM
$saved=$user->save();
if($saved){ echo '[success]';}else{echo '[error]';}/*http://stackoverflow.com/a/27878102/1883256*/
})->name('your.url.post');
Now, from the view side, make your ajax like this:
/*Referencias: https://www.codecourse.com/forum/topics/use-jquery-ajax-post-with-the-authentication-series/527*/
/*Permissions*/
$(function() {
$('#your_element').change(function(){
var var1 = this.val1;
var var2 = this.val2;
console.log('Value1 is: '+var1+' and value2 is: '+var2);
$.ajax({
data: {var1: var1,var2: var2,{{ csrf_key }}: "{{ csrf_token }}"},
type: "POST",
dataType: "json",
headers: { "cache-control": "no-cache" },
url: "/your/url" //add ../your/url if needed
});//ajax end
});
});
});
That's it.
It is also important that you send the csrf token if you're using it in your forms.