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.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘