简体   繁体   中英

Cannot send POST data via FormData with AJAX if version of Jquery is 1.4

I have a service, which looks like this:

 values = new FormData();
 values.append("mpsRegnomer", $('#mpsRegnomer').val());
 alues.append("mpsMarka", $('#mpsMarka').val());

 $.ajax({
     type: 'post',
     url: 'assets/clients/services/test.php',
     async: false,
     contentType: false,
     processData: false,
     cache: false,
     dataType: "JSON",
     data: values,
     success:function(response){....

And in test.php:

var_dump($_POST);

Well if version of jquery is 1.10 or 1.12 $_POST is fill with data, but when jquery is 1.4.. $_POST is empty

Where am I wrong ?

PS:

for(var pair of values.entries()) {
   console.log(pair[0]+ ', '+ pair[1]); 
}

returns me same values for every version.

The problem you have is because contentType is only available from jQuery 1.6 and newer. This property is required when serialising FormData to ensure that it's encoded within the request properly. As you don't have access to it in jQuery 1.4, you cannot send FormData in an AJAX request.

However the bigger point is why are you using jQuery 1.4? It's nearly 10 years out of date.

Also, note that async: false is incredibly bad practice and should not be used. As you've defined a success handler function you don't need a synchronous request anyway

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM