I'm getting a 404 (Not found) in the console when trying to get data with AJAX in my Slim PHP application. Here's the error message :
http://localhost:8888/Project/mods/public/edit-mod/ajax/get-categories?gameID=1 404 (Not Found)
Here's the route defined in a routes.php file (that is correctly included, all the other routes are working) :
$app->get("/ajax/get-categories/", function() use ($app, $User, $Game, $Mod){
//Fetch data and echo it
});
Finally, here's how I'm calling the AJAX page in the JS script :
$.get("ajax/get-categories", {gameID: gameID}, function(data){
//Do something with data
});
I tried changing the Slim route to "ajax/get-categories/" (without the leading /
) but it didn't change anything, and I also try a bunch of different paths for the AJAX call (in the JS script) but nothing worked, I always get the 404 no matter what.
When I'm calling only ajax/get-categories
in my script, it seems to be appending the current page (ex edit-mod/
) to the route, maybe that's my problem.
Is there a way to match every route that ends with ajax/get-categories
, so that both upload/ajax/get-categories
and edit-mod/ajax/get-categories
will work?
Let me know if you need any more code, I think I've included everything that is relevant to the problem.
I haven't worked with Slim framework. But I looked up in the documentation and I think that's not how you should pass parameters to the GET request.
in your route, change your code to something like this:
$app->get("/ajax/get-categories/:gameID", function($gameID) use ($app, $User, $Game, $Mod){
// You can use the query string $gameID here...
var_dump($gameID);
// Do other stuff here...
});
In your JavaScript file:
// I assume in there is a gameID variable in your JavaScript.
$.get("ajax/get-categories/" + gameID, function(data) {
//Do something with data
});
Tell me if it works.
See the documentation here.
$app->get("/:segment/ajax/get-categories", function($segment) use ($app, $User, $Game, $Mod){
//Fetch data and echo it
});
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:
<script type="text/javascript">
/*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
});
});
});
</script>
That's it.