Laravel项目服务器端提交含格式/图片的表单报405错误求助
Laravel部署表单提交405错误问题
本地环境一切正常,但在服务器上提交表单时,只要在文本编辑器中使用text-align对齐或插入图片,就会返回405 Method Not Allowed错误。
已尝试的解决方案
- 将路由改为match/any方法:不再返回405,但提交的payload为空
- 通过Ajax请求提交表单
- 调整中间件配置
- 通过.htaccess禁用ModSecurity
- 更换TinyMCE/CKEDITOR等多款富文本编辑器
以上操作均未解决问题。
路由配置
Route::get('/dashboard/new-project', 'BackofficeController@getNewProject')->name('backoffice.new-project'); Route::post('/dashboard/projects', 'BackofficeController@createNewProject')->name('process.create.new.project');
控制器代码
public function getNewProject() { $project_type = Project_type::all(); $products = Product::with([ 'sub_category' => function ($query) { $query->with([ 'sub_category_languages' => function ($query) { $query->where('language_id', '=', 1); }, 'category' => function ($query) { $query->with([ 'category_languages' => function ($query) { $query->where('language_id', '=', 1); } ]); } ]); } ]) ->where('is_active', '=', true) ->where('is_deleted', '=', false) ->orderBy('name', 'ASC') ->get(); return view('backoffice.new-project', compact('project_type', 'products')); } public function createNewProject(Request $request) { $request->validate([ 'slug' => 'required', 'category' => 'required', 'published_at' => 'required | date', 'is_active' => 'required |boolean', 'title' => 'required', 'description' ]); if (Projects::where('slug', request('slug'))->exists()) { return back()->with('msg', 'This title already exists')->withInput(); } else { $project = Request([ 'slug' => 'slug', 'category' => 'category', 'meta_description' => 'meta_description', 'key_words' => 'key_words', 'published_at' => 'published_at', 'is_active' => 'is_active' ]); // save project in DB Projects::create($project); } $project_languages = Request([ 'title' => 'title', 'sub_title' => 'sub_title', 'description' => 'description' ]); // save project language in DB $lastProject = BackofficeRepository::getLastProject(); $projectId = $lastProject->id; $project_languages['projects_id'] = $projectId; Projects_languages::create($project_languages); // save products relashionatips in DB foreach ($request->get('products') as $product_id) { $projects_products = new Projects_products(); $projects_products->product_id = $product_id; $projects_products->projects_id = $projectId; $projects_products->save(); } // thumbnail image upload $path = $request->file('image'); $custom_name_file = $request->slug . '-' . 'thumbnail' . '.jpg'; $folder_thumbnail = 'img/inspirations/thumbnails/' . $request->slug; $path->move($folder_thumbnail, $custom_name_file); // banners upload $files = $request->file('banner'); $folder_banners = 'img/inspirations/banners/' . $request->slug; if (!empty($files)): $i = 0; foreach ($files as $file): $i++; $custom_name_banner = $request->slug . '-' . 'banner' . '-'. $i . '.jpg'; $file->move($folder_banners, $custom_name_banner); endforeach; endif; return redirect('/dashboard/all-inspirations')->with('msg', 'Project created success!'); }
表单及Ajax代码
<div class="container-fluid"> <div class="row m-0"> <div class="col-12"> <div class="col-12 my-3"> <h3 class="mb-2 text-uppercase" style="color: #89bab6;font-size:2rem !important;text-align:center"><b>Create a new project</b></h3> </div> <div class="col-md-12"> <form id="form-create" method="post" accept-charset="utf-8" enctype="multipart/form-data"> @csrf <div class="row m-0"> <div class="form-group col-6 pl-0 pr-2"> <label for="title">Title:</label> <input type="text" class="form-control" value="{{ old('title') }}" id="title" name="title" placeholder="Insert title to project" onblur="showSlug()" required> </div> <div class="form-group col-6 pr-0 pl-2"> <label for="sub_title">Sub Title:</label> <input type="sub_title" class="form-control" value="{{ old('sub_title') }}" id="sub_title" name="sub_title" placeholder="Insert sub title to project"> </div> </div> <div class="form-group d-none"> <label for="slug">Slug:</label> <input type="text" class="form-control" value="{{ old('slug') }}" id="slug" name="slug" placeholder="Insert the slug to project.> </div> <div class="form-group"> <textarea name="description" id="description" rows="15" placeholder="Insert your project content">{{ old('description') }}</textarea> </div> <div class="form-group"> <label for="date">Date:</label> <input type="date" class="form-control" value="{{ old('date', date('Y-m-d')) }}" id="published_at" name="published_at" required> </div> <div class="form-group row m-0"> <label for="is_active" class="p-0">Category:</label> @foreach($project_type as $category) <div class="form-group col-4 d-flex"> <input type="checkbox" name="category[]" id="category" value="{{$category->name}} {{ old('category[]') }}" style="display:block;width:15px"> <p class="ml-3 mb-0">{{$category->name}} </p> </div> @endforeach </div> <div class="form-group"> <label for="products">Select products:</label> <select class="select-product" required multiple="multiple" name="products[]" id="products" data-live-search="true" data-maximum-selection-length="4"> @foreach ($products as $product) <option value="{{$product->id}}"> {{$product->name}} {{$product->sub_category->sub_category_languages[0]->name}} </option> @endforeach </select> </div> <div class="form-group"> <label for="meta">Meta Description:</label> <textarea name="meta_description" id="meta_description" cols="30" rows="2" maxlength="200" placeholder="Insert the meta description to project">{{ old('meta_description') }}</textarea> <div class="d-flex justify-content-end align-items-center"> <span id="charCounter1" style="text-transform:lowercase;">200</span> </div> </div> <div class="form-group"> <label for="key_words">Key Words:</label> <textarea name="key_words" id="key_words" cols="30" rows="2" maxlength="200" placeholder="Insert the key words to project">{{ old('key_words') }}</textarea> <div class="d-flex justify-content-end align-items-center"> <span id="charCounter2" style="text-transform:lowercase;"></span> </div> </div> <div class="form-group row m-0"> <label for="is_active" class="p-0">Project is actived?</label> <div class="form-group col-4 d-flex"> <input type="checkbox" name="is_active" id="is_active" onclick="checkedBox(this)" value="1" style="display:block;width:15px" checked> <p class="ml-3 mb-0">Yes</p> </div> <div class="form-group col-4 d-flex"> <input type="checkbox" name="is_active" id="is_active" onclick="checkedBox(this)" value="0" style="display:block;width:15px"> <p class="ml-3 mb-0">No</p> </div> </div> <div class="form-group"> <div class="row m-0"> <div class="col-8 p-0"> <label for="image">Thumbnail image: *please insert a image with dimensions: 850x950</label> <input type="file" class="form-control" id="image" name="image" accept="image/*" required> </div> <div class="col-4 px-4"> <img src="" alt="" class="preview-img" style="max-width: 89px; max-height: 100px"> </div> </div> </div> <div class="form-group"> <label for="image">Banner image: *please insert a image with dimensions: 500x600 or 1200x600</label> <input type="file" class="form-control" id="banner" name="banner[]" accept="image/*" multiple required> </div> <div class="col-12 p-0 m-auto d-flex justify-content-between"> <a class="btn-primary btn" href="{{url()->previous()}}" style="border-radius: 0.50rem; padding: 10px 24px"> < BACK </a> <button class="btn bg-gradient-success" type="submit" id="btn-ajax" aria-hidden="true">SAVE</button> </div> </form> </div> </div> </div> </div> @endsection @section('footer-scripts') <script src="https://cdn.tiny.cloud/1/my9ztv3a3drx46ktolxf8hdqvfrd28mzr2tolcfv2vdwy24u/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script> <script> $(document).ready(function(){ $('#btn-ajax').click(function(e){ e.preventDefault(); var formData = new FormData(document.getElementById("form-create")); console.log(formData); var content = tinymce.get("description").getContent(); var url = '{{route("process.create.new.project")}}'; $.ajax({ url: url, method: 'POST', data: formData, dataType: 'JSON', contentType: false, cache: false, processData: false, success: function(response) { alert(response.success) }, error: function(response, error) { console.log(response) alert(error) } }); }); }); </script>
即使直接通过Laravel原生表单提交,也会出现相同问题,恳请帮忙解决。
内容的提问来源于stack exchange,提问作者Thiago Alves
相关产品推荐
相关产品推荐

