AJAX - 404与超薄PHP [英] AJAX - 404 with Slim PHP

查看:110
本文介绍了AJAX - 404与超薄PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Slim PHP应用程序中尝试使用AJAX获取数据时,我在控制台中获得了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)

这是在routes.php文件中定义的路由(正确包含,所有其他路线都有效):

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
});

最后,这是我在JS脚本中调用AJAX页面的方式:

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    
}); 

我尝试将Slim路线更改为ajax / get-categories /(不含前导 / )但它没有改变任何东西,我也尝试了一堆不同的AJAX调用路径(在JS脚本中)但没有任何效果,我总是得到404没有无论如何。

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.

当我在我的脚本中只调用 ajax / get-categories 时,似乎是将当前页面(ex edit-mod / )附加到路线,这可能是我的问题。

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.

有没有办法匹配以 ajax / get-categories 结尾的每条路线,以便两者都 upload / ajax / get-categories edit-mod / ajax / get-categories 会有效吗?

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.

推荐答案

我使用过Slim Framework;)

I have worked with Slim Framework ;)

看,这就是我设法让Ajax与Slim框架一起工作的方式。

Look, this is how I have managed to make Ajax work with Slim framework.

首先,你必须声明路径两者 get 发布(当我第一次尝试声明发布时,它不起作用):

First, you have to declare the routes both get and post (when I first tried declaring post only, it didn't work):

获取:

$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');

现在,从视角来看,像这样制作你的ajax:

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>

就是这样。


  • 如果您在表单中使用csrf令牌,那么发送它也很重要。

这篇关于AJAX - 404与超薄PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆