【问题标题】:AJAX - 404 with Slim PHPAJAX - 404 与 Slim PHP
【发布时间】:2015-07-28 12:36:01
【问题描述】:

在我的 Slim PHP 应用程序中尝试使用 AJAX 获取数据时,我在控制台中收到 404(未找到)。这是错误消息:

http://localhost:8888/Project/mods/public/edit-mod/ajax/get-categories?gameID=1  404 (Not Found)

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

$app->get("/ajax/get-categories/", function() use ($app, $User, $Game, $Mod){

    //Fetch data and echo it
});

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

$.get("ajax/get-categories", {gameID: gameID}, function(data){

    //Do something with data    
}); 

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

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

有没有办法匹配以 ajax/get-categories 结尾的每条路线,以便 upload/ajax/get-categoriesedit-mod/ajax/get-categories 都可以工作?

如果您需要更多代码,请告诉我,我想我已经包含了与问题相关的所有内容。

【问题讨论】:

标签: javascript php jquery ajax slim


【解决方案1】:

我没有使用过 Slim 框架。但我查阅了文档,我认为这不是您应该将参数传递给 GET 请求的方式。

在您的路线中,将您的代码更改为以下内容:

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

在您的 JavaScript 文件中:

// I assume in there is a gameID variable in your JavaScript.
$.get("ajax/get-categories/" + gameID, function(data) {

    //Do something with data    
}); 

告诉我它是否有效。

请参阅文档here

【讨论】:

    【解决方案2】:
    $app->get("/:segment/ajax/get-categories", function($segment) use ($app, $User, $Game, $Mod){
    
        //Fetch data and echo it
    });
    

    【讨论】:

      【解决方案3】:

      我使用过 Slim 框架;)

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

      首先,您必须声明 both getpost 路线(当我第一次尝试声明 post只是,它不起作用):

      获取:

      $app->get('/your/url',$permission(),function() use ($app){
        //here you can check whether it is an ajax call
      })->name('your.url');
      

      发布:

      $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 像这样:

      <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 令牌,发送它也很重要。

      【讨论】:

        猜你喜欢
        • 2017-04-21
        • 2015-07-08
        • 1970-01-01
        • 1970-01-01
        • 2016-04-23
        • 2012-10-20
        • 1970-01-01
        • 2015-08-13
        • 1970-01-01
        相关资源
        最近更新 更多