简体   繁体   English

405方法不允许-带有Laravel路由的Ajax请求

[英]405 Method not allowed - ajax request with laravel route

Calling an ajax function should be done this way and I have done it multiple times.This time I keep getting 405 method not allowed .I know this issue is known but I have tried suggested solutions and still nothing works. 调用ajax函数应该以这种方式完成,我已经完成了多次。这一次我一直在使用405 method not allowed 。我知道这个问题是已知的,但是我尝试了建议的解决方案,但仍然没有任何效果。 Here's my controller: 这是我的控制器:

public function playGame($custom_url)
    {
        $project = Project::where('custom_url', $custom_url)->where('approved', 1)->first();
        if(Request::isMethod('post'))
        {
            $project->play_count = $project->play_count+1;
            $project->save();
            return json_encode(array('status'=>array('saved')));
        }
        return View::make('game.index');
    }

index.blade.php: index.blade.php:

<title>Tennra</title>
        <link rel="shortcut icon" href="favicon.png"/>

        <link href="{{URL::asset('game/style.css')}}" rel="stylesheet" type='text/css'>
        <link href="{{URL::asset('game/menu.css')}}" rel="stylesheet" type='text/css'>
        <script type="text/javascript" src="{{URL::asset('game/jq.js')}}"></script>
        <script type="text/javascript" src="{{URL::asset('game/mainMenu.js')}}"></script>
        <script type="text/javascript" src="{{URL::asset('game/game.js')}}"></script>
        <script type="text/javascript" src="{{URL::asset('game/main.js')}}"></script>
    </head>

    <body>

    </body>

games.js: games.js:

function startedGame() {
    /*********************************
    **********************************
    ***   Add Code Here if Needed  ***
    **********************************
    *********************************/

    $.ajax({
        type: "post",
        data: null,
        url: "{{URL::route('projects.game')}}"
      }).done(function( msg )
      {
        msg=JSON.parse(msg);
        if(msg['errors'])
        {
        }
        else if(msg['status'])
        {
            console.log(true);
        }
      });
}

Got it. 得到它了。 When I create ajax requests in blade file url is always in this form: 当我在刀片文件中创建ajax请求时,URL始终采用以下形式:

url: "{{URL::route('urlname')}}"

But since the request was in .js file, the syntax was no longer correct so I had to update it as follows: 但是由于请求位于.js文件中,因此语法不再正确,因此我必须按以下方式进行更新:

url: window.location

As the post request had the same url as the current window. 由于发布请求的网址与当前窗口相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM