NadeemAkhter's avatar

Upload image through ajax

I want to submit image through ajax request without using FormData class.

var data1={'file':$("#file").val(),};  
    $.ajax({
            url:'/insertCar',
            type:'POST',
            dataType: 'json',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            data:data1,
            success:function(data){
               if($.isEmptyObject(data.error)){
                   console.log(data);
                   $("#myModal").modal();
                    }else{
               printErrorMsg(data.error);
                                
                    }
            }
        });
0 likes
3 replies
developre's avatar

Use jquery file upload plugin to post the image.

rawilk's avatar

Or use dropzone. You can't upload files with Ajax without using FormData.

2 likes
cmdobueno's avatar

Id love to win the lottery without buying a ticket, but some things just are not possible. Without form-data AJAX can not upload a file. Even if you use a jquery file upload plugin (or dropzone) they all still use form data.

The real question is why do you not want to use form data?

1 like

Please or to participate in this conversation.