【问题标题】:I want to delete data without refreshing whole page using ajax我想删除数据而不使用 ajax 刷新整个页面
【发布时间】:2020-05-22 11:13:05
【问题描述】:

我是 Laravel 的新手,我正在尝试使用 ajax 删除数据,当我单击删除按钮时,页面正在刷新,但数据正在完美删除,但不应重新加载。

控制器

public function destroy($id)
{
     $delete = Digitizing::where('id', $id)->delete();
     return back();
}
 

HTML 视图

<a  href="{{route('digitizing.delete',$digitizing->id)}}"   
            class="btn btn-danger" onclick="deleteConfirmation({{$digitizing->id}})">Delete</a>



<script type="text/javascript">
                    
     function deleteConfirmation(id) {
         Swal.fire({
              title: "Delete?",
              text: "Please ensure and then confirm!",
              type: "warning",
              showCancelButton: !0,
              confirmButtonText: "Yes, delete it!",
              cancelButtonText: "No, cancel!",
              reverseButtons: !0
              }).then(function (e) {

                  if (e.value === true) {
                       var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

                       $.ajax({
                       type: 'POST',
                       url: "{{url('digitizing/delete')}}/" + id,
                       data: {_token: CSRF_TOKEN},
                       dataType: 'JSON',
                       success: function (results) {
                       if (results.success === true) {
                        swal("Done!", results.message, "success");
                       } else {
                         swal("Error!", results.message, "error");
                       }
                   }
              });
          } else {
              e.dismiss;
          }
      }, function (dismiss) {
           return false;
      })
   } 
</script>

【问题讨论】:

  • 你好祖拜尔。快速提醒您,您的许多问题都带有令人尴尬的乞求。读者宁愿质疑作者不要进行长时间的恳求,尤其是考虑到这里的大多数人都是志愿者。请坚持技术写作。

标签: laravel


【解决方案1】:
**Delete wants a get method because you have to give only ID to delete.**
**WEB.PHP**
    Route::get('digitizing/delete/{id}','YourController@destroy');
    **SCRIPT**
        let id = $('#my_id').val();
        $.ajax({
            type: 'GET',
            url: "{{url('digitizing/delete')}}/" + id,
            data: {
                _token: '{{csrf_token()}}',
            },
            success: function () {
               alert('Successfully Deleted');
            }
    }).fail(function(){
      console.log('problem with route = digitizing/delete');
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2017-01-23
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多