【问题标题】:update not working using ajax call in laravel在 laravel 中使用 ajax 调用更新不起作用
【发布时间】:2018-04-01 12:43:46
【问题描述】:

我正在尝试更新我的“部分”项目列表,但它只更新第一个项目,并且更新不适用于列表中的其他项目,并获得除第一个项目外的“MethodNotAllowedException”。我没有找到原因。请有人帮我找出原因吗?

index.blade.php

<td class="text-center">
  <a href="#" data-toggle="modal" data-target="#editModal-{{ $section->id }}"> <i class="fa fa-pencil text-warning m-r-10"></i> </a>

  <!-- Edit Modal -->
  <div class="modal fade" id="editModal-{{ $section->id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1">
   <div class="modal-dialog" role="document">
        <div class="modal-content">
             <form id="editForm" method="POST">

                   @method('PUT')

                   @csrf

             <div class="modal-header text-left">
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                   <h4 class="modal-title" id="exampleModalLabel1">Update Section</h4>
             </div>
              <div class="modal-body">

                    <div id="updatesuccess-msg" class="hide text-left">
                    <div class="alert alert-success alert-dismissible fade in" role="alert">
                     <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                     </button>
                        <strong>Success:</strong> Section Updated !
                    </div>
                    </div>
                    <div class="form-group text-left">
                         <label for="section" class="control-label">Section Name:</label>
                         <input type="text" name="section" value="{{ $section->section }}" class="form-control" >
                         <input type="hidden" name="section_id" class="sec_id" value="{{ $section->id }}">
                         <span class="text-danger">
                              <small id="updateinput-error"></small>
                         </span>
                     </div>

                     </div>
                   <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Save</button>
                   </div>
           </form>
       </div>
    </div>
  </div>                   

阿贾克斯

$("#editForm").click(function(event) {
event.preventDefault();

var form = $("#editForm");
var formData = form.serialize();
var sectionId = parseInt($('.sec_id').val());
       $( '#input-error' ).html( "" );

       $.ajax({
           url:'{{ url("admin/section") }}' + '/' + sectionId,
           method:'PUT',
           data:formData,
           success:function(data) {
            console.log(data);
            if(data.errors) {
                   if(data.errors.section){
                       $( '#updateinput-error' ).html( data.errors.section[0] );
                   }

            }
            if(data.success) {
                $('#updatesuccess-msg').removeClass('hide');
                    setInterval(function(){ 
                       $('#editForm').modal('hide');
                       $('#updatesuccess-msg').addClass('hide');
                       location.reload();
             }, 1000);
        }
    },
});

});

SectionController.php

public function update(Request $request, $id)
 {
        $validator = Validator::make($request->all(), [
              'section' => 'required|max:255|unique:sections',
          ]);

          $sectionId = $request->input('section_id');
          if ($validator->passes()) {

             // Update your section in database
             $data = Section::find($sectionId);
             $data->section = $request->section;
             $data->save();

            return Response::json(['success' => '1']);
            //return response()->json($data);

          }

  return Response::json(['errors' => $validator->errors()]);
 }

web.php

Route::group(['prefix' => 'admin', 'middleware' => ['auth:admin'], 'as' => 'admin.'], function () { 
     Route::resource('section', 'Admin\HR\SectionController'); 
});

【问题讨论】:

  • 试过$("#editForm").submit(function(event) { ?
  • 是的,我试过了。
  • var sectionId = parseInt($('.sec_id').val());总是得到 1 作为它的值。
  • @RashedHasan 你修好了么???

标签: php jquery ajax laravel methods


【解决方案1】:

将ajax方法改为POST

method:'POST'

【讨论】:

  • 首先我尝试使用 ajax 方法 'POST',它不起作用,有人告诉我更改为 'PUT',所以我尝试了 'POST' 和 'PUT'。但没有工作。
  • 必须是POST方式。 update() 方法必须使用 POST 方法和隐藏的 _method 字段中的 PUT 参数调用。还有其他问题。
  • 更改为“POST”方法。提交除第一个项目以外的其他项目时,浏览器网络显示它没有获得“id”。可能这就是我收到“MethodNotAllowed”异常的原因。
【解决方案2】:

改用 axios..

//update
axios.patch(url, {}).... //PATCH

//create
axios.post(url, {}).... //POST | PUT -> axios.put(url, {})

//get
axios.get(url, {})....  //GET

//delete
axios.delete(url, {}).... //DELETE

对于您的问题.. 尝试从 PUT 更改为 PATCH

查看这个资源控制器 laravel..https://laravel.com/docs/5.6/controllers#resource-controllers

【讨论】:

  • 我尝试了 PUT 和 PATCH。 (当我将 'editForm' id 更改为 'editForm' 表单类时,它正在运行。但是,现在它为列表中的每个项目获取第一个 ID 值为 1 并且不更新。
  • 如果我要axios,那么我必须使用vuejs,不是吗?我尝试使用 vuejs,但我使用的是一个使用 bootstrap 4 的付费管理模板,其中 jquery 版本有'jQuery v3.1.1',我的 app.js 有 jquery 3.3.7,我遇到了很多冲突。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多