MethodNotAllowedHttpException [AJAX]!

时间:2017-08-01 21:57:42

标签: php jquery ajax laravel routes

我的路线有些问题。

Code: MethodNotAllowedHttpException in RouteCollection.php line 219.

我使用ajax和laravel 5.1。我尝试使用谷歌,但我不明白,我尝试方法"路径",并在ajax更改"键入"到"方法"。我尝试GET,但没有工作。

我的阿贾克斯:

    $.ajax({
    type: 'POST',
    url: '/placeBet',
    dataType: 'json',
    data: {
        ammount: ammount,
        color: color
    },
    headers: {
        'X-CSRF-TOKEN': $('#_token').val()
    },
    success: function (data) {
        if (data['color'] == 'red') {
            var currentPlaced = parseInt($('.user_red').html());
            currentPlaced = currentPlaced + parseInt(data['ammount']);
            $('.user_red').html(currentPlaced);
        }
        if (data['placedMuch']) {
            alertify.error('Max 4 bets in round!');
        }
        if (data['color'] == 'purple') {
            var currentPlaced = parseInt($('.user_black').html());
            currentPlaced = currentPlaced + parseInt(data['ammount']);
            $('.user_black').html(currentPlaced);
        }
        if (data['color'] == 'gold') {
            var currentPlaced = parseInt($('.user_gold').html());
            currentPlaced = currentPlaced + parseInt(data['ammount']);
            $('.user_gold').html(currentPlaced);
        }
        if (data['color'] == 'green') {
            var currentPlaced = parseInt($('.user_green').html());
            currentPlaced = currentPlaced + parseInt(data['ammount']);
            $('.user_green').html(currentPlaced);
        }
        if (data['baaad'] == true) {
            alertify.error('Niu niu!');
            return;
        }
        if (data['improvements'] == true) {
            alertify.error('We are working on improvements.');
            return;
        }
        if (data['toLow'] == true) {
            alertify.error('Minimum bet is 200 diamonds!');
            return;
        }
        if (data['can_bet'] == false) {
            alertify.error('You have withdraw request pending, you cannot bet!');
            return;
        }

        if (data['placed'] != false && data['coins'] != false) {
            var coins = data['coins'];
            var htmlo = '<div class="chat_message"><div class="top"><div class="right_info"><a href="javascript:void(0);" class="user_name admin">Info Bot</a></div></div><div class="message">You spent ' + ammount + ' diamonds on ' + data['color'] + '.</div></div>';
            $('#chatmessages').append(htmlo);
            $('.recents_box').mCustomScrollbar("scrollTo", 'bottom');
            $({countNum: $('#currentBallance').html()}).animate({countNum: coins}, {
                duration: 1000,
                easing: 'linear',
                step: function () {
                    $('#currentBallance').html(parseFloat(this.countNum).toFixed(0))
                },
                complete: function () {
                    $('#currentBallance').html(parseFloat(this.countNum).toFixed(0))
                }
            });
        } else if (data['placed'] != false && data['coins'] == '0') {
            var coins = data['coins'];
            var htmlo = '<div class="chat_message"><div class="top"><div class="right_info"><a href="javascript:void(0);" class="user_name admin">Info Bot</a></div></div><div class="message">You spent all your diamonds on ' + data['color'] + '.</div></div>';
            $('#chatmessages').append(htmlo);
            ('.recents_box').mCustomScrollbar("scrollTo", 'bottom');
            $({countNum: $('#currentBallance').html()}).animate({countNum: coins}, {
                duration: 1000,
                easing: 'linear',
                step: function () {
                    $('#currentBallance').html(parseFloat(this.countNum).toFixed(0))
                },
                complete: function () {
                    $('#currentBallance').html(parseFloat(this.countNum).toFixed(0))
                }
            });

            // swal('Yea', 'You placed a bet to ' + color + '. Your current coins are : ' + coins + '!', 'success');
            // $('#betammount').val(0);
        } else {
            if (data['logged'] == false) {
                alertify.error('You need to be log in to use this option!');
            } else if (data['coins'] == false) {
                alertify.error('You are not that rich!');
                // $('#betammount').val(0);
            } else if (data['coins'] == '0') {
                alertify.error('You are empty!');
                //$('#betammount').val(0);
            }

        }
    }

});

我的路线:

 Route::post('/placeBet', ['as' => 'placeBet', 'uses' => 'RouletteController@placeBet']);

我的RoulleteController:

    public function placeBet(Request $request)
{

    $info = [];

    if (Auth::check()) {

        $lastID = DB::select("SELECT * FROM roulette_history ORDER BY id DESC LIMIT 1");
        $roundID = $lastID[0]->id;
        $steamID = Auth::user()->steamId64;
        $ammount = $request->All()['ammount'];
        $getCountPlaced = \App\placedBets::where('gameID', $roundID)->where('userID64', Auth::user()->steamId64)->count();
        if($getCountPlaced > 3) {
            $info['placedMuch'] = true;
            $info = json_encode($info);
            return $info;
        }
        if($ammount < 1 || !is_numeric($ammount)) {
            $info['baaad'] = true;
            $info = json_encode($info);
            return $info;
        }
        if ($ammount < 200) {
            $info['toLow'] = true;
            $info = json_encode($info);
            return $info;
        }
        if(Auth::user()->global_banned > 0) {
            $info['baaad'] = true;
            $info = json_encode($info);
            return $info;
        }
        $color = $request->All()['color'];
        if($color == 'black') {
            $color = 'purple';
        }

        $user_info = \App\User::where('steamId64', Auth::user()->steamId64)->first();
        $active_ofer = \App\ofers::where('userID', Auth::user()->steamId64)->count();
        $info['active_ofer'] = $active_ofer;

        $user_coins = $user_info->coins;
        if ($user_coins < $ammount) {
            $info['coins'] = false;
            $info['placed'] = false;

        } else {
            $user_coins = $user_coins - $ammount;
            $siteProfit = \App\profit::first();
            $siteProfitTotal = $siteProfit->siteProfit;
            $siteProfitTotal = intval($siteProfitTotal) + intval($ammount);
            $updateProfit = DB::update("UPDATE siteProfit SET siteProfit='$siteProfitTotal'");
            $user_update = \App\User::where('steamId64', Auth::user()->steamId64)->update(['coins' => $user_coins]);

            $info['coins'] = $user_coins;
            $active_bet = DB::select("SELECT * FROM roulette_history ORDER BY id DESC LIMIT 1");
            $game_id = $active_bet[0]->id;
            $placed = DB::insert('insert into placed_bets (color, userID64, ammount, gameID, avatar,url,nick,isStreamer,streamLink) values (?, ?, ?, ?,?,?,?,?,?)', [$color, $steamID, $ammount, $game_id, $user_info->avatar, $user_info->url, $user_info->nick,$user_info->isStreamer,$user_info->steamLink]);
            $info['placed'] = true;
            $info['ammount'] = $ammount;
            $info['color'] = $color;
            $info['count'] = $getCountPlaced;
        }
        $info['logged'] = true;
    } else {
        $info['coins'] = 0;
        $info['logged'] = false;
        $info['placed'] = false;
    }
    $info = json_encode($info);
    return $info;
}

如果你需要消息来源,我可以帮到我们!

1 个答案:

答案 0 :(得分:1)

在你的ajax电话中,改变

$.ajax({
    type: 'POST',

$.ajax({
    method: 'POST',