I am attempting to upload a file through jQuery to my remote server, but I can't seem to get it working.
The user needs to be able to upload a pdf file, which will then be processed by the server and saved to the root folder.
jQuery
$('form').submit(function (e) {
var fd = new FormData();
fd.append('file', $('#file')[0].files[0]);
$.ajax({
cache: false,
beforeSend: function (xhr) {
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("pragma", "no-cache");
},
url: 'http://www.codekraken.com/testing/pointify/test.php?callback=?',
data: fd,
dataType: "json",
processData: false,
contentType: false,
type: 'POST',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
e.preventDefault();
});
PHP
<?php
header('content-type: application/json; charset=utf-8');
$name = $_FILES["fd"]["name"];
echo ($_GET['callback'] . '('.json_encode($name).')');
?>
HTML
<form>
<input type="file" id="file" name="file">
<input type="submit">
</form>
When I submit a file, such as input.pdf and then press submit, I get the response (null). I would expect to get the name of the file, input.pdf, which means I am missing a crucial step in this process.
localhosthowever, if that matters...callback=?in the url makes ajsonprequest. Which is a GET not a POST, you would want to make a POST when uploading files.null, not an object containingnull.