weixin_33717298 2015-05-16 17:53 采纳率: 0%
浏览 40

AJAX-带有Slim PHP的404

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.

  • 写回答

3条回答 默认 最新

  • MAO-EYE 2015-05-16 18:29
    关注

    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.

    评论

报告相同问题?