1

I am using laravel 5.4 and jquery Ajax to upload file and some form data.

I am using below code

function submitDocument(){
    var formData = new FormData(); // Currently empty
    var _token = $("#_token").val().trim();
    formData.append('title', $("#title").val());
    formData.append("doc",$("#doc")[0].files[0]);
    $.ajax({
      url: "documents",
      method: "post",
      data:{_token,formData},
    }).done(function(data) {

    });
    return false;// Not to submit page
}

And I am getting error

Uncaught TypeError: Illegal invocation

enter image description here

How can I fix this ? Thanks in advance for your time.

I am able to get value in formData by using

console.log(formData.get('title'));
console.log(formData.get('doc'));

enter image description here

3
  • Have you tried adding the _token value inside the formData object? Commented Nov 18, 2017 at 8:18
  • @gbalduzzi : Yes I tried, No succes. Commented Nov 18, 2017 at 8:35
  • Seems like a duplicate: stackoverflow.com/questions/6974684/… Commented Nov 21, 2017 at 16:02

3 Answers 3

3
+50

Try adding processData: false, contentType: false in your code

Replace your script with this:

function submitDocument(){
var formData = new FormData(); // Currently empty
var _token = $("#_token").val().trim();
formData.append('title', $("#title").val());
formData.append("doc",$("#doc")[0].files[0]);
$.ajax({
  url: "documents",
  method: "post",
  data:{_token,formData},
  cache : false,
  processData: false,
  contentType: false
}).done(function(data) {

});
return false;// Not to submit page
}

By default, data passed in to the data option as an object will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

Sign up to request clarification or add additional context in comments.

4 Comments

My code is not going to the controller , Because of using processData: false, I am getting "Token mismatch Error"
Use this headers after contentType: false headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') },
I tried with this but not getting any value of title or doc
maaaan you just saved my *ss
1
<script>
$(document).ready(function() {
    var url = "{{ url('/admin/file') }}"; 
    var options = { 
            type: 'post',
            url: url,       
            headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}'},
            dataType: 'doc',
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                alert('Ok');
            },
            error: function (data) {
                alert('Error');
            }
    }; 
    $('#save').on('click', function() { 
        $("#form").ajaxSubmit(options); 
        return false; 
    }); 
}); 
</script>

Comments

0

Try this way

$(document).ready(function (){
$("#form").on('submit',(function(e){
        e.preventDefault();
        var formdata = new FormData(this);
        var _token = $("#_token").val().trim();
        formData.append('title', $("#title").val());
        formData.append("doc",$("#doc")[0].files[0]);
        $.ajax({
            url: "/site/url",
            type: "POST",
            data:{token:_token,formData},
            contentType: false,
            cache: false,
            processData:false,
            success: function(data){

            },
        });
    }));});

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.