方法不允许 Http 异常 Ajax 请求

我正在尝试通过 laravel 5 中的 ajax 更新一些数据,但我面临 MethodNotAllowed Http 异常。


所以,我在 web.php 中创建了一个资源路由,一个 HTML 表单,用于在文本框中预填充一些数据,以及一些 ajax 代码。


Web.php 的代码,前缀为 admin。


Route::resource('settings', 'OrganisationSettingsController', ['only' => ['edit', 'update', 'index', 'change-language']]);


Html 表单代码


{!! Form::open(['id'=>'editSettings','class'=>'ajax-form','method'=>'PUT']) !!}

//Input Elements Goes Here...

                                    {!! Form::close() !!} Ajax 调用代码


$('#save-form').click(function () {

    $.easyAjax({

        url: '{{ route('admin.settings.update', ['1']) }}',

        container: '#editSettings',

        type: "POST",

        redirect: true,

        file: (document.getElementById("logo").files.length != 0 || document.getElementById("login_background").files.length != 0) ? true : false

    })

});

当用户单击更新按钮时,数据必须更新,但我在浏览器的控制台中得到了 http 方法不允许异常。


繁星点点滴滴
浏览 119回答 3
3回答

ibeautiful

您将需要从 ajax 调用中欺骗您的方法,假设easyAjax允许它应包括的数据属性:data: {&nbsp; &nbsp; '_method' : 'PUT'},此外,您需要包含您的 csrf 令牌:data: {&nbsp; &nbsp; '_method' : 'PUT',&nbsp; &nbsp; '_token' : '{{csrf_token()}}'},注意: '{{csrf_token()}}'只要脚本是刀片视图的一部分,就可以工作。如果没有,则使用标题:headers: {&nbsp; &nbsp; 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')}如果您需要使用标头来包含您的 csrf 令牌,请确保在文档的头部部分包含元标记:<meta name="csrf-token" content="{{ csrf_token() }}">完整的解决方案:$('#save-form').click(function () {&nbsp; &nbsp; $.easyAjax({&nbsp; &nbsp; &nbsp; &nbsp; url: '{{ route('admin.settings.update', ['1']) }}',&nbsp; &nbsp; &nbsp; &nbsp; container: '#editSettings',&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '_method' : 'PUT',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '_token' : '{{csrf_token()}}'&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; redirect: true,&nbsp; &nbsp; &nbsp; &nbsp; file: (document.getElementById("logo").files.length != 0 ||&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("login_background").files.length != 0) ? true : false&nbsp; &nbsp; })});

慕田峪9158850

您正在发布数据。当您在路由器中发布数据并使用资源时,laravel 会调用 store 函数,而您的路由不允许(['only' => ['edit', 'update', 'index', 'change-language']).为了测试它,将商店功能添加到您的路线中,dd($request);您可以看到您的request;

蝴蝶刀刀

这正是问题所在。最后我写了下面的代码来得到预期的结果$('#save-form').click(function () {&nbsp; &nbsp; &nbsp; &nbsp; $.easyAjax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '{{ route('admin.settings.update', ['1']) }}',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container: '#editSettings',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'company_name': $('#company_name').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'company_email': $('#company_email').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'company_phone': $('#company_phone').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'website': $('#website').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'address': $('#address').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'currency_id': $('#currency_id').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'timezone': $('#timezone').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'locale': $('#locale').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'latitude': $('#latitude').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'longitude': $('#longitude').val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '_method' : 'PUT',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '_token' : '{{csrf_token()}}'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redirect: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file: (document.getElementById("logo").files.length != 0 || document.getElementById("login_background").files.length != 0) ? true : false&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP