doudu2591 2018-06-12 01:25
浏览 90
已采纳

来自AJAX的内部服务器错误

I have a ul that consists of lis which are sortable i.e drag and droppable, I put the ids in an array and try to send to my controller through AJAX but it keeps on saying Internal server error please what may be the problem

This is my HTML

@if(isset($images_ext)&& !empty($images_ext))
      <ul class="reorder1 row">
                                @foreach ($images_ext as $image)
                                    <li class="img-box" data-src="{{$image->filename}}" id="{{$image->id}}">
                                        <div class="img-w" style="background-image: url('{{$image->filename}}')">
                                            {{-- <a href=""> <img class="mb-2 uploaded-photos " src="{{$image->filename}}" alt=""></a>!--}}
                                        </div>
                                        <span style="color: #333333;position: relative;width: 100%;text-align: justify;
         display: inline;">{{$image->description}} <i class="fa fa-upload" style="margin-left: 10px; color:#333333;"></i></span>
                                    </li>
                                @endforeach
                            </ul>
                        @endif

This is my ajax code

$.ajax({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        type: "POST",
                       url: "{{ action('ImageController@update') }}",
                        data: {ids: " " + h + ""},
                        dataType: 'json',
                        success: function () {
                            console.log(h);
                        }
                    });

This is my Route

Route::post('/settings/orderphotos', 'ImageController@update')->name('settings.updatephotos');

This is my controller

 public function update(Request $request){
    $data = [];
      $count = 1;
        // Get images id and generate ids array
        $id_array = $request->ids;
        $data["success"]= $id_array;
     //   dd($id_array);
        foreach ($id_array as $id) {
            $image = Image::findOrFail($id);
            $image->update([
                'image_order' => $count
            ]);
            $count++;



        }
    return json_encode('status', 'Image order saved');

}
  • 写回答

1条回答 默认 最新

  • dpbfb7119 2018-06-12 18:28
    关注

    The problem is with your json_encode() function call. You are using it wrong. If you want to send status back to the client, do it this way.
    Replace

    return json_encode('status', 'Image order saved');
    

    with this code

    $response = ['status' => 'Image order saved'];
    return json_encode($response);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line