I am using the Slim PHP Framework and am trying to send FormData using Jquery.ajax() in the following way:
var data = new FormData();
data.append('some_name', 'some_data');
data.append('a_file', $('input[name=the_file_form_field]').get(0).files[0]));
$.ajax({
url: 'the_destination_url',
data: data,
processData: false,
contentType: false,
type: 'PUT',
dataType: 'json',
success: function(data, textStatus, jqXHR) {
//Processing result here
},
error: function(jqXHR, textStatus, errorThrown) {
//Processing result here
}
});
However, I tried the following scenarios:
Is there anything I'm missing?
the application requires it to use a PUT request and as such a POST request is no possibility.
Just try to send data as an object. eg
data: {'formData' : data},
Your Ajax request should look like
$.ajax({
url: 'the_destination_url',
data: {'formData' : data},
processData: false,
contentType: false,
type: 'PUT',
dataType: 'json',
success: function(data, textStatus, jqXHR) {
//Processing result here
},
error: function(jqXHR, textStatus, errorThrown) {
//Processing result here
}
});
I solved it using the comments from people posting in: https://bugs.php.net/bug.php?id=26004
Apparently PHP silently discards all incoming POST data whenever the post_max_size
directive is exceeded.
Aside from post_max_size
it's also smart to check the upload_max_filesize
directive.
The Best way to get PUT data sent from Ajax JQuery in PHP is:
Send your data Like this in JavaScript:
where mydata is {k1 : "val1" , "k2" : "val2" , }
$.ajax({
url: "data.php",
contentType : "json",
data : mydata,
method:"PUT",
success: function (data) {
console.log(data);
},
error: function (err) {
console.log(err);
},
complete:function (e) {
console.log(e);
}
});
The way to get the data in data.php file is:
parse_str(file_get_contents("php://input"),$putVars);
var_dump( $putVars ); // input from your form
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.