图像已删除,但页面未刷新ajax laravel

时间:2017-10-30 14:41:31

标签: ajax laravel

您好我在点击按钮删除时遇到问题它删除了里面有图片的文件夹,这就是我想要的但页面没有刷新,所以我不知道删除直到我点击刷新 这是代码:

            @if($question->question_image) 
            <img src="{{url('/')}}/images/questions/{{ $question->id }}/medium/{{ $question->question_image }}"  class="answer-image-create" id="question_image_box">
 <input onchange="readquestionURL(this);" type="file" id="question_file" name="question_image" style="display:none"/>
            @else
            <img src="{{ asset('noavatars/no-image-selected.png') }}" class="answer-image-create" id="question_image_box">
             <input onchange="readquestionURL(this);" type="file" id="question_file" name="question_image" style="display:none"/>
            @endif 
          </div>

    {{--       <button type="button" class="btn" id="question_upfile"
                  style="cursor:pointer; padding-left: 24px; padding-right: 15px;">
            <i class="fa fa-upload" aria-hidden="true">
            </i>@lang('buttons.upload_image')
          </button> --}}

            <button type="button" value="{{-- {{ $i }} --}}" class="btn btn-flat btn-default btn-sm delete_image" id="delete_image" title="@lang('buttons.remove_option')"><i class="fa fa-trash-o" aria-hidden="true"></i> </button>

和javascript代码是这样的:

<script>

        $(document).on("click", "button[id=delete_image]", function(data){
       var questionid = {{$question->id}};
       var element = 'questions';

       $.ajax({
        type: "POST",
        url: "{{ action('website\images\ImagesController@deleteImage') }}",
        data: {
            _token: "{{ csrf_token() }}",
          'id':questionid,
                'element' : element,

      },
       success: function(response) {
        console.log(response);
    },
    error: function(response){
        alert("fail");
    }

      });

         function readquestionURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#question_image_box')
                    .attr('src', e.target.result)
            };

            reader.readAsDataURL(input.files[0]);
            }
        }
    });




    </script>

控制器是:

 public function deleteImage($element =null, $id = null){
          if(request()->ajax()) {

            $id = request()->input('id');
            $element = request()->input('element');

        if($element != null && $id != null){
             Storage::disk('public')->deleteDirectory('/images/'.$element. '/'. $id);
          }
        return 1;
        }
    }

如何删除图像并同时刷新。有人可以帮助我,非常感谢你。

1 个答案:

答案 0 :(得分:2)

您可以使用javascript刷新页面

location.reload();

您需要在ajax的success方法中添加该行。