-3

I am using laravel homestead. I am making a game that requires credits on the account of which it is played on. I want to make sure that after every play the credits of the user gets updated through an ajax request, however with this ajax request, I get the same error which is PATCH http://gamesite.test/updateBalance/13 419 (unknown status) if I change the data it gets the error: The GET method is not supported for this route. Supported methods: PATCH.

I already tried to change the methods of the ajax request and it is working on other pages.

The ajax request that I made is the following:

$(oMain).on("save_score", function(evt,iMoney) {
    if(getParamValue('ctl-arcade') === "true"){
        parent.__ctlArcadeSaveScore({score:iMoney});
    }
    //...ADD YOUR CODE HERE EVENTUALLY
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax({
        url: 'updateBalance/'+{{ auth()->user()->id }},
        type: 'PATCH',
        data: {iMoney:iMoney, _method: "PATCH"},
        success: function(res) {
        }
    });
});

I expected that it would update the users credits, instead got the error: "PATCH http://gamesite.test/updateBalance/13 419 (unknown status)"

EDIT: Route:

Route::patch('/updateBalance/{id}', 'GamesController@updateBalance');

GamesController:

public function updateBalance(User $id) {
    $selecteduser = User::find($id)->first();
    $this->validate(request(), [
        'credit' => 'int'
    ]);
    $selecteduser->credit = request('iMoney');

    $selecteduser->save();
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
GJvKl
  • 25
  • 10

2 Answers2

0

Found the answer, I needed to add to the header in the blade.

GJvKl
  • 25
  • 10
-1

use HTTP default GET method, this works fine.

$.get (url, {iMoney:iMoney, _method: "PATCH"} , function(){
   //success
})
rahul varma
  • 31
  • 1
  • 8
  • it doesn't work, if I use it like that, it would give the error get method not allowed if I only use .get instead of .ajax, I would get the 419 error. – GJvKl Oct 02 '19 at 18:20
  • Check it out, I think you will find a solution https://stackoverflow.com/questions/11461414/ajax-json-doesnt-get-sent-in-patch-only/13439828 – rahul varma Oct 02 '19 at 18:31