简体   繁体   English

Laravel 错误 405(不允许使用方法)Ajax 发帖

[英]Laravel error 405(Method Not Allowed) Ajax Posting

Hello i wnat to send my data with ajax to my controller.你好,我想用 ajax 将我的数据发送到我的控制器。

My CODE我的代码

AJAX 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网络响应

I just removed the slash at the end of url and it began working... /managers/games/id/push/ to:我刚刚删除了 url 末尾的斜杠,它开始工作...... /managers/games/id/push/到:

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

add this one in your layout.blade file在你的 layout.blade 文件中添加这个

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

then use this one in your js code然后在你的 js 代码中使用这个

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

i hope this will help!!我希望这个能帮上忙!!

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.由于您在 JavaScript 文件中工作,而不是在 Blade 文件中工作,因此route()助手方法不起作用,并且路由“race.post”未解析为 url。

Try to change the url to this:尝试将 url 更改为:

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 .当您想在 JavaScript 中使用route()帮助程序时,您必须将脚本添加到 Blade 文件中,并对值进行 json_encode,您可以在此答案中阅读有关此内容的更多信息。

I have different way to use it: AJAX我有不同的使用方式: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希望对你有帮助

The URL you're posting to doesn't look right in the console output you posted.您发布的 URL 在您发布的控制台输出中看起来不正确。 In your AJAX code, you have this:在你的 AJAX 代码中,你有这个:

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.但这只是按原样解释,并没有被解释为 Laravel 中该路由的值。

You'll need to make sure that your JavaScript code is in a Blade template if you want Blade tags parsed.如果你想解析 Blade 标签,你需要确保你的 JavaScript 代码在 Blade 模板中。

not type: "POST", method :'POST" try the below code i have modified. ref: Reference Link HTML code不输入:“POST”,方法:'POST”尝试下面我修改过的代码。参考:参考链接HTML代码

<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");
    });
}

First thing is we put two routes in one for displaying view and another for post ajax.第一件事是我们将两个路由放在一个用于显示视图,另一个用于 post ajax。 So simple add both routes in your route file.如此简单,在你的路由文件中添加两条路由。

routes/web.php路线/ 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在 ajax 调用中包含 javascript 代码

$.ajaxSetup({

    headers: {

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

    }

}); 

Check if your laravel route is correctly set for this request.检查您的 Laravel 路由是否为此请求正确设置。 In my case, I had a $.ajax url: "crop-image-upload" and a Route::post('crop-image-upload ', 'CropImageController@uploadCropImage');就我而言,我有一个 $.ajax url: "crop-image-upload"和一个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');但是请求被发送到http://127.0.0.1:8000/news/crop-image-upload所以我不得不将我的路由更改为Route::post('/news/crop-image-upload ', 'CropImageController@uploadCropImage');

So, in your case, try to add a literal url on ajax like this:因此,在您的情况下,尝试在 ajax 上添加一个文字 url,如下所示:

url:"/races/updateC"

and add 'races/' in the route like this:并在路线中添加“races/”,如下所示:

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

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

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