Wednesday, March 1, 2017

send File and Data in Ajax

I'm trying to send file and data together to Controller when I send File it work correctly but When I try to send data and file nothing I get.

                contentType: false,
                processData: false,

and as I remove this codes I can send data what should I do?

                 <div class="form-group ">
                  <input class="form-control" id="title" name="title" type="text"/>

                 </div>
                 <div class="form-group ">
                  <textarea class="form-control" cols="40" id="desc" name="textarea" rows="10"></textarea>
                 </div>
                 <div class="form-group ">

                  <div class="">
                   <div class="radio">
                    <label class="radio">
                     <input name="answer"  id="respawn" type="radio" value="1"/>
                     1
                    </label>
                   </div>
                   <div class="radio">
                    <label class="radio">
                     <input name="answer" type="radio" value="2"/>
                     2
                    </label>
                   </div>
                   <div class="radio">
                    <label class="radio">
                     <input name="answer" type="radio" value="3"/>
                    3
                    </label>
                   </div>
                  </div>
                 </div>
                 <div class="form-group">
                <input type="file" name="file"  id="file">
                 </div>

controller

 public function create(Request $request)
  {
    dd($request->all());


    return response()->json([
    'success' => true,

]);
  }

ajax $("#offer").click(function(){

    var title=$("#title").val();
    var desc=$("#desc").val();
    var respawn=$("#respawn").val();
    var pic = new FormData();
    pic.append( 'file', $( '#file' )[0].files[0] );


    var offer ={
        title:title,
        desc:desc,
        respawn:respawn,
        pic:pic
    };

    $.ajax({
        method:'POST',
        url:'/create',
        headers: {
            'X-CSRF-TOKEN': CSRF_TOKEN
        },
        data:formData,
        contentType: false,
        processData: false,
        success:function(response){
            alert("Successful!")
        }
    });




});




via Linton Samuel Dawson

Advertisement