0

i try to make upload image with ajax without form, when i am sent data without the image, data successfully submitted to the database, but when i am adding an image, when i try to submit, no response at all,

this is my template code :

 <input type="hidden" name="lesson_id" value="{{-- $lessons->id --}}">
   <input type="hidden" name="parent_id" value="0"> -->
    <div class="form-group">
      <label>Komentar</label>
      <textarea rows="8" cols="80" class="form-control" name="body" id="textbody0"></textarea>
    </div>
   <ul class="right">
  <input type="file" name="image" id="image" />
  <img id="myImg" src="#" />
 <button type="button" class="btn btn-primary" onClick="doComment({{ $lessons->id }},0)" >Kirim</button> 

and this is my script for submit data :

function doComment(lesson_id, parent_id) {

    var body = $('#textbody'+parent_id).val();
    var image = $('#image').prop('files')[0];

    if (body == '') {
      alert('Harap Isi Komentar !')
    }else {


      var postData =
                  {
                      "_token":"{{ csrf_token() }}",
                      "lesson_id": lesson_id,
                      "parent_id": parent_id,
                      "image": image,
                      "body": body
                  }
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="token"]').attr('value')
        }
    });
      $.ajax({
          type    :'POST',
          url     :'{{ url("lessons/coments/doComment") }}',
          dataType: 'json',
          data    : postData,
          beforeSend: function(){
            // Show image container
            swal({
                title: "Sedang mengirim Komentar",
                text: "Mohon Tunggu sebentar",
                imageUrl: "{{ asset('template/web/img/loading.gif') }}",
                showConfirmButton: false,
                allowOutsideClick: false
              });
              {{--  $("#loader").show();  --}}
          },
          success:function(data){
            if (data.success == false) {
               window.location.href = '{{ url("member/signin") }}';
            }else if (data.success == true) {
              $('#textbody'+parent_id).val('');
              swal({
                title: "Komentar anda sudah terkirim!",
                showConfirmButton: true,
                timer: 3000
              });

              getComments();
            }
          }
      });
    }
  }

Can any one help me?

1

1 Answer 1

0
postData = new FormData();
if(!!file.type.match(/image.*/)){
  postData.append("image", file);
  $.ajax({
    type    : 'POST',
    url     : '{{ url("lessons/coments/doComment") }}',
    data    : postData,
    dataType: 'json',
    processData: false,
    contentType: false,
    success: function(data){
        alert('success');
    }
  });
}else{
  alert('Not a valid image!');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.