0

I have a script that should update a user's post without rebooting. But the form data, for some reason, is not transferred through the formData object, everywhere is null, except for those fields that are manually registered in the controller (id, user_id). What can be wrong?

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

$('.infinite-scroll').on('click', '#editPostButton', function(e) {
    e.preventDefault();

    var id = $(this).data('id');
    var user_id = $('#userForm').val();
    var form = document.getElementById('EditPostForm'+id);
    var formData = new FormData(form);

    $.ajax({
        url: "id"+user_id+"/"+id+"/edit",
        type: "PATCH",
        data: formData,
        success: function(data) {
            console.log(data);
            $("#textpostdata"+id).html($(data).find("#textpostdata"+id).html());
            $("#closeButton"+id).click();
        },
        error: function() {
            console.log('error');
        },
        contentType: false,
        processData: false,
    });

});

And my controller

public function editPost(storeRequest $request, $id, $postId) {

    $user = User::find($id);

    if(!$user && $user != Auth::user()->id) {
        return abort(404);
    }

    $post = Profile::find($postId);

    if(!$post) {
        return abort(404);
    }

    $post->user_id = Auth::user()->id;
    $post->title = $request->title;
    $post->message = $request->message;
    $post->videoPost = str_replace('watch?v=', 'embed/', $request->videoPost);

    if($request->file('img')) {
        $path = Storage::putFile('public/' . Auth::user()->id . '/post', $request->file('img'));
        $url = Storage::url($path);
        $post->img = $url;
    }

    $post->update();

    //return redirect()->back();
    return $post;
}
  • https://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery or https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax – Rwd Sep 26 '20 at 09:44
  • what do you mean by "without rebooting"? Can you post the payload sent to controller from AJAX? – Belisario Peró Sep 26 '20 at 15:09

0 Answers0