title: ajax laravel not inserting or code not working
body: <p>i want to insert to the database by ajax crud i am inserting but the code dosn't work iam using jquery javascript framework</p>
  <p>i want to insert to the database by ajax crud i am inserting but the code dosn't work iam using jquery javascript framework</p>
  <p>i dont' know why although i am writing correctly
  please help
  here is my code
  my blade file</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
      &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
      &lt;title&gt;Document&lt;/title&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;{{asset('css/app.css')}}&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;div class=&quot;row&quot; style=&quot;matgin-top:50px;&quot;&gt;
              &lt;div class=&quot;col-md-6&quot;&gt;
                  &lt;div class=&quot;card&quot;&gt;
                      &lt;div class=&quot;card-hehader bg-primary text-white&quot;&gt;Add new product&lt;/div&gt;
                      &lt;form id=&quot;form&quot; action=&quot;{{route('products.store')}}&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
                          @csrf
                          @method('POST')
                          &lt;div class=&quot;form-group&quot;&gt;
                              &lt;label for=&quot;&quot;&gt;product name&lt;/label&gt;
                              &lt;input type=&quot;text&quot; name=&quot;name&quot; class=&quot;form-control&quot;&gt;
                              &lt;span class=&quot;text-danger error-text product_name_error&quot;&gt;&lt;/span&gt;
  
                          &lt;/div&gt;
                          &lt;div class=&quot;form-group&quot;&gt;
                              &lt;label for=&quot;&quot;&gt;product image&lt;/label&gt;
                              &lt;input type=&quot;file&quot; name=&quot;image&quot; class=&quot;form-control&quot;&gt;
                              &lt;span class=&quot;text-danger error-text product_name_error&quot;&gt;&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;button type=&quot;submit&quot; class=&quot;btn btn-info&quot;&gt;add&lt;/button&gt;
                      &lt;/form&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class=&quot;col-md-6&quot;&gt;
                  &lt;div class=&quot;card&quot;&gt;
                      &lt;div class=&quot;card-header bg-primary&quot;&gt;All Products&lt;/div&gt;
                      &lt;div class=&quot;card-body&quot;&gt;
  
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;script src=&quot;{{asset('js/app.js')}}&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js&quot;&gt;&lt;/script&gt;
      &lt;script&gt;
          $(document).ready(function(){
              $('#form').on('submit',function(e){
                  e.preventDefault();
                  var form = this;
                  $.ajax({
                      url:$(form).attr('action'),
                      method:$(form).attr('method'),
                      data:new FormData(form),
                      processData:false,
                      dataType:'json',
                      contentType:false,
                      beforeSend:function(){
                          $(form).find('span.error-text').text('');
                      },
                      succes:function(data){
                          if(data.code == 0){
                              $.each(data.error, function(prefix,val){
                                  $(form).find('span.'+prefix+'_error').text(val[0]);
                              });
                          }
                          else{
                              $(form)[0].reset();
                              alert(data.msg);
                              // fetchAllProducts();
                          }
                      }    
                  })
              });
              
          });
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>and my controller store function</p>
  <pre><code>public function store(Request $request)
  {
           $path = 'files/';
           $file = $request-&gt;file('product_image');
           $file_name = time().'_'.$file-&gt;getClientOriginalName();
  
        //    $upload = $file-&gt;storeAs($path, $file_name);
        $upload = $file-&gt;storeAs($path, $file_name, 'public');
  
           if($upload){
               Product::insert([
                   'name'=&gt;$request-&gt;name,
                   'image'=&gt;$request-&gt;image,
               ]);
               return response()-&gt;json(['code'=&gt;1,'msg'=&gt;'New product has been saved successfully']);
           }
       
  }
  </code></pre>