Reputation: 2050
When i make requests this way i never get the response to manipulate . Well i get as response ***Array ( [name] => Mastering React Native (2).pdf [type] => application/pdf ... but i need to show to user the file info.
<form id='formid'>
<input id='fileid' type='file' name='file1' hidden/>
<input type='submit' value='IMPORT'/>
</form>
<script>
$(document).on("submit", "form", function (e) {
e.preventDefault();
var fdat = new FormData(this);
fdat.append('operation', 'IMPORT');
function importFile() {
return $.ajax({
url: "data-source/oracle_controller_upload.php",
type: 'POST',
dataType: "JSON",
data: fdat,
processData: false,
contentType: false,
success: function(response){
}
});
}
debugger;
importFile().done(function (result) {
}).fail(function () {
});
});
</script>
this is my php code:
if (@$_REQUEST['operation'] === "IMPORT" && $_FILES) {
echo print_r($_FILES['file1']);
return;
}
Cant reach either the success callback or done(). Any help?
Upvotes: 0
Views: 39
Reputation: 2050
Solved with
if (@$_REQUEST['operation'] === "IMPORT" && $_FILES) {
echo json_encode($_FILES['file1']);
return;
}
I have dataType: "JSON"
, so of course the response must be JSON.
Upvotes: 1
Reputation: 61914
You haven't said exactly, but I think you must be getting an error about an invalid JSON response. Your ajax request is expecting JSON back from the server, but you're just sending a raw string dump of your variable.
In the PHP, do this:
if (@$_REQUEST['operation'] === "IMPORT" && $_FILES) {
echo json_encode($_FILES['file1']); //encode the object as JSON instead of just a variable dump
return;
}
and in your .done() method:
importFile().done(function (result) {
alert(result.name); //just for example, to display the name in an alert
//...etc
Upvotes: 1