kvithalani's avatar

Uncaught TypeError: Illegal invocation

Hello guys,

I am trying to store image using AJAX call in laravel. While I am requesting it is giving me an error of JQuery. I want to store my image on S3 bucket. I have done:

HTML code:

<input type="file" class="" name="image" id="image{{$sub->id}}" accept="image/*">
<button type="button" class="btn btn-success updateVal" id="update" value="{{$sub->id}}">Update</button>

JS code:

$('.updateVal').click(function() {
 var id = $(this).val();
 var parent_id = "{{$productAttributeValue->id}}";
 var valueType = 'sub';
 var _token = $('input[name="_token"]').val();
 var subValue = $('#value'+id).val();
 var subValImage = $('#image'+id).val();
 var theFormFile = $('#image'+id).get()[0].files[0];
 console.log(theFormFile);
 var data = {_token: _token, mainVlaue: parent_id, valueType: valueType, value: subValue, image: theFormFile, id:id, _method: 'PUT'};
 console.log(data);
       $.ajax({
           url: '/admin/update/sub-product-attribute-value/'+id,
           type: 'PUT',
           data: data,
           dataType: 'json',
           success:function(response)
           {
             if(response.code == 200) {
               window.location.reload();
             } else {

             }
              //
           }, error: function(response) {
             alert(response);
           }
           });
});

I got one solution that give this -> processData: false, into AJAX request. But it is not passing data in request at laravel side. Can anyone knows another solution. Thanks in advance..

0 likes
6 replies
jaythanki's avatar

@kvithalani you need to add enctype in ajax as below

$.ajax({
           url: '/admin/update/sub-product-attribute-value/'+id,
           type: 'PUT',
           data: data,
           enctype: 'multipart/form-data' // ADD THIS 
           dataType: 'json',
           success:function(response)
           {
             if(response.code == 200) {
               window.location.reload();
             } else {

             }
           }, error: function(response) {
             alert(response);
           }
});
1 like
jaythanki's avatar

@kvithalani also add to your form tag

<form class="m-form " method="POST" action="" id="create_form" enctype="multipart/form-data" data-toggle="validator">

Please or to participate in this conversation.