Patrik
Patrik

Reputation: 51

Laravel error 405(Method Not Allowed) Ajax Posting

Hello i wnat to send my data with ajax to my controller.

My CODE

AJAX

  $.ajax( {
    type:'POST',
    header:{
      'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content')
    },
    url:"{{route('race.post')}}",
    data:{
      _token: "{{ csrf_token() }}",
      dataType: 'json', 
      contentType:'application/json', 
    }


})
.done(function() {
    alert('success');
})
.fail(function() {
    alert("error");
});

CONTROLLER

 public function Points(Request $request){
    $test = $request->input('data');
    return "$test";
}

ROUTE

Route::post('updateC', ['uses' =>'RacesController@Points', 'as' => 'race.post']); 

And there are the errors what i get.

Console

Network-preview

Network-Response

Upvotes: 3

Views: 23249

Answers (9)

Roma Debrian
Roma Debrian

Reputation: 21

        $.ajax({
            url: `***/****/`,
            type: "POST",
            cache: false,
            data: {
                "title": title,
                "content": content,
                '_token': '{{ csrf_token() }}',
                '_method': 'PUT',
            },
            success: function(response) {},
            error: function(error) {}
        });

OR

<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

Upvotes: 0

Giovanni Stroppa
Giovanni Stroppa

Reputation: 41

Check if your laravel route is correctly set for this request. In my case, I had a $.ajax url: "crop-image-upload" and a Route::post('crop-image-upload ', 'CropImageController@uploadCropImage'); But the request was sent to http://127.0.0.1:8000/news/crop-image-upload So I had to change my route to Route::post('/news/crop-image-upload ', 'CropImageController@uploadCropImage');

So, in your case, try to add a literal url on ajax like this:

url:"/races/updateC"

and add 'races/' in the route like this:

Route::post('/races/updateC', ['uses' =>'RacesController@Points', 'as' => 'race.post']); 

Upvotes: 0

Optimaz Prime
Optimaz Prime

Reputation: 937

First thing is we put two routes in one for displaying view and another for post ajax. So simple add both routes in your route file.

routes/web.php

Route::get('ajaxRequest', 'RacesController@Points');

Route::post('ajaxRequest', 'RacesController@Points');

Include this meta tag inside your view

<meta name="csrf-token" content="{{ csrf_token() }}" />

Include javascript code inside your ajax call

$.ajaxSetup({

    headers: {

        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

    }

}); 

Upvotes: 1

Harry Bosh
Harry Bosh

Reputation: 3800

I just removed the slash at the end of url and it began working... /managers/games/id/push/ to:

$http({
  method: 'POST',
  url: "/managers/games/id/push",

Upvotes: 8

sandip bharadva
sandip bharadva

Reputation: 646

add this one in your layout.blade file

<meta name="csrf-token" content="{{ csrf_token() }}">

then use this one in your js code

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

i hope this will help!!

Upvotes: 1

ThataL
ThataL

Reputation: 165

not type: "POST", method :'POST" try the below code i have modified. ref: Reference Link HTML code

<button onClick="onBtnClick()" data-url="{{route('race.post')}}"></button>

Updated Code

function onBtnClick(){
    var token = $('meta[name="csrf-token"]').attr('content');
    var url = $(this).attr("data-url");    
    $.ajax( {
        method:'POST',
        header:{
          'X-CSRF-TOKEN': token
        },
        url: url,
        data:{
          _token: token,
          dataType: 'json', 
          contentType:'application/json', 
        }        
    })
    .done(function() {
        alert('success');
    })
    .fail(function() {
        alert("error");
    });
}

Upvotes: 0

Martin Bean
Martin Bean

Reputation: 39429

The URL you’re posting to doesn’t look right in the console output you posted. In your AJAX code, you have this:

url:"{{route('race.post')}}"

But that’s just getting interpreted as is, it’s not getting interpreted as the value of that route in Laravel.

You’ll need to make sure that your JavaScript code is in a Blade template if you want Blade tags parsed.

Upvotes: 0

Kinjal
Kinjal

Reputation: 91

I have different way to use it: AJAX

data = {
        selectmanufacturer: selectmanufacturer,
        categories: selectCategory,
        _token: "{{csrf_token()}}",
        productName: productName
      };
     $.ajax({
        url: '{{URL::to('/all-products-data')}}',
        type: 'POST',
        dataType: 'json',
        data: data,
        success: function (response) {
        },
          error: function (response) {
            alert(response);
          }
        });

Controller:

public function Points(Request $request){
    $test = $request->all();
    return "$test";
}

I hope It will be helpful to you

Upvotes: 0

piscator
piscator

Reputation: 8739

Since you are working in a JavaScript file and not in a Blade file, the route() helper method is not working, and the route 'race.post' isn't parsed to an url.

Try to change the url to this:

url: '/updateC'

When you want to use the route() helper in your JavaScript, you have to add the script to a Blade file, and json_encode the value, you can read more about this in this answer.

Upvotes: 0

Related Questions