2016-07-21 26 views
0

私のPHPサーバーにフォームを送信しようとしていますが、Laravelの検証でフォームが空であると表示されます。私は写真アルバムをアップロードしようとしています。私のフォームは、タイトル(テキスト)、表紙(画像)、写真(画像)で構成されています。 422エラー(Unprocessable Entity)が表示されます。以下のコードは、Laravel:フォームをAjax経由で送信

HTML

<form class="lajax" action="{{ action('[email protected]') }}" method="POST"> 
    <div class="form-group"> 
     <label>Album Name</label> 
     <input type="text" name="name" class="form-control">             
    </div> 

    <div class="form-group"> 
     <label for="coverFile">Album Cover Image</label> 
     <input name="cover" type="file" id="coverFile"> 
     <p class="help-block">Example block-level help text here.</p> 
    </div> 

    <div class="form-group"> 
     <label for="albumFiles">Album Images</label> 
     <input type="file" name="photos[]" multiple> 
    </div> 

    <button type="submit" class="btn btn-primary">Create Album</button> 
</form> 

jquerysアヤックス方法に供給されるAJAX //オブジェクトの責任のjQuery

  var ajax_options={ 
        url: url, 
        method: method, 
        beforeSend: function(jqXHR,settings){ 
         console.log(jqXHR); 
         console.log(settings); 
         if(optns.debug) 
          console.log('executing beforeSend function'); 
         optns.lajaxBeforeSend($form,formData,jqXHR,settings); 
        }, 
        success: function(data,textStatus,jqXHR){ 
         if(optns.debug) 
          console.log('executing success function'); 
         optns.lajaxSuccess($form,formData,data,textStatus,jqXHR) 
        }, 
        error: function(jqXHR,textStatus,errorThrown){ 
         if(optns.debug) 
          console.log('error encountered. ajax error function procked'); 
         optns.lajaxError($form,formData,jqXHR,textStatus,errorThrown); 

         var errors = jqXHR.responseJSON; 
         console.log(errors); 
        }, 
       } 

       //check if files are included in the submitted form if the method is not GET 
       if($form.find('input:file').length && method!='GET'){ 
        ajax_options.processData=false; 
        ajax_options.contentType=false; 
        ajax_options.cache=false; 
        ajax_options.data=formData; 
       } 


       if(optns.debug) 
        console.log('About to send ajax request'); 

       //sending request here 
       $.ajax(ajax_options); 

laravelのPHPファイル

public function store(Request $request) 
    { 
     //request input verification rules 
     $rules=[ 
     'name'=>'required', 
     'cover'=>'required|image', 
     'photos'=>'required|array', 
     'photos.*'=>'image' 
     ]; 

     //perform validation 
     $this->validate($request,$rules); 

     //rest of code 
    } 
+0

リクエストがサーバーに期待どおりに送信されている場合は、ブラウザのコンソールでチェックインしましたか?それは私が最初に見ているところです – Theson

+0

5月

+0

@IlyaYaremchukそれはajaxを通して送信された場合に違いがありますか? – alaboudi

答えて

0

追加してみてください enctype="multipart/form-data" files=true をフォームタグ

関連する問題