简体   繁体   中英

Internal server error from AJAX

I have a ul that consists of li s which are sortable ie 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');

}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM