Usando JSON
Pues yo lo he hecho transformando el archivo en Base64 y mandandolo como string
Por ejemplo:
function getBase64(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
console.log(reader.result);
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
}
Y tu json quedaria mas o menos asi:
"data:text/plain;base64,aG9sYQ=="
La configuración que uso es la siguiente:
El form que tenga enctype="multipart/form-data"
y de preferencia que quede en POST para que no pase algo raro, quedando:
<form action="" method="post" enctype="multipart/form-data">
<input type="text" id="nombre">
<button type="submit">enviar</button>
</form>
Removí accept-charset="utf-8"
no se si afecte, pero puedes hacer tus pruebas y contarnos.
Y en nuestro código javascript
$('#nuestroForm').on('submit',(function(e) {
e.preventDefault(); //importante
var formData = new FormData(this);
$.ajax({
type:'POST',
url: 'nuestra url',
data:formData,
cache:false,
contentType: false,
processData: false,
success:function(data){
console.log("success");
console.log(data);
},
error: function(data){
console.log("error");
console.log(data);
}
});
}));
Mira que yo no tengo dataType: 'json' y asi funciona.
Documentación de ajax en jQuery
processData (default: true)
Type: Boolean By default, data passed in
to the data option as an object (technically, anything other than a
string) will be processed and transformed into a query string, fitting
to the default content-type "application/x-www-form-urlencoded". If
you want to send a DOMDocument, or other non-processed data, set this
option to false.
contentType (default: 'application/x-www-form-urlencoded;
charset=UTF-8')
Type: Boolean or String
When sending data to the
server, use this content type. Default is
"application/x-www-form-urlencoded; charset=UTF-8", which is fine for
most cases. If you explicitly pass in a content-type to $.ajax(), then
it is always sent to the server (even if no data is sent). As of
jQuery 1.6 you can pass false to tell jQuery to not set any content
type header. Note: The W3C XMLHttpRequest specification dictates that
the charset is always UTF-8; specifying another charset will not force
the browser to change the encoding. Note: For cross-domain requests,
setting the content type to anything other than
application/x-www-form-urlencoded, multipart/form-data, or text/plain
will trigger the browser to send a preflight OPTIONS request to the
server.
data
del ajax pones dato sin la s