Why content-type false AJAX - JQUERY

Asked

Viewed 1,317 times

2

I am sending files via AJAX with Jquery, using the FormData(), and then a question arose, why does it work:

$.ajax({
  url:"action/receipt.php",
  method:"POST",
  processData: false,
  contentType: false,
  data: form,
  success:function(data){

  }

});

and that’s not?

$.ajax({
  url:"action/receipt.php",
  method:"POST",
  processData: false,
  data: form,
  success:function(data){

  }

});

I mean, the first way the variable $_FILES prints the expected values, in the second, it is empty.

Why not fill in the content-type can lead to $_FILES empty? Would it be because the pattern be in a different sending format than multipart/form-data, or I am traveling?

2 answers

2

Yes, that’s right. If you check the request actually performed by the browser (using the development tools) you will see that the Content-Type standard is the application/x-www-form-urlencoded, whose server-side processing is completely different from multipart/form-data.

2


When you use the contentType with the value false, you are forcing jQuery not to pass the default value to the contentType request. The default, as quoted above, is application/x-www-form-urlencoded.

Probably, when it is done with in the example below, the jQuery will import this contenttype information directly from form.

var f = new FormData($('#form').get(0));

$.ajax({ data: f, contentType: false, cache: false, processData: false})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.