dsf4354353452 2019-04-07 07:36
浏览 92

不允许使用Laravel删除方法

I am writing a laravel api and when I try to make delete and post requests I keep getting a method not allowed exception. Where should I be looking to diagnose this problem?

I have read though most of the other posts on this issue and here is what I have tried/looked at. • Made sure the route syntax was correct

• Made sure it didn't conflict with another route

• Made sure I was using the correct route (ran php artisan route:list to double check)

• Modified the .htaccess folder (maybe I did this incorrectly) to allow GET, POST, PUT, DELETE

Here is what the route looks like in api.php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});
Route::delete('delete/{id}', 'LoginController@delete');
Route::get('stuff', 'LoginController@index');
Route::get('stuff1/{Username}', 'LoginController@show');

here is the function in the controller

 public function delete(Request $request, $id) {

        $user = Login::find($id);
        $user->delete();
        return "204";
    }

here is my .htaccess

   <IfModule mod_rewrite.c>
   <IfModule mod_negotiation.c>
       Options -MultiViews
   </IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

 <Limit GET POST PUT DELETE>
 Allow from all
</Limit>
</IfModule>

I can get around this issue by changing Route::delete() to Route::get() and achieve the same functionality but this doesn't seem like standard practice.

  • 写回答

1条回答 默认 最新

  • dqspy04266 2019-04-07 07:46
    关注

    You have to set ajax type POST but send a parameter named _method with value delete like this:

    $.ajax({
            type: "POST",
            data:{
             _method:"DELETE"
            },
            url: productRoute,
            headers: { 'X-CSRF-TOKEN' : productToken }
        });
    

    Or because you use laravel html form helper so it generates _method hidden input automatically so you'd better send all your form inputs such as token and method like this:

     function()
    {
    
        var formData=$('#yourForm').serialize();
        $.ajax({
            type: "POST",
            url: productRoute,
            data:formData
        })
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度