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 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统