Html: {code ...} backend php part: ajax. php {code ...} the js part is successfully uploaded using js, but there are two errors when using jquery: one is using $. ajax method: {code ...} the error message says the pic in $ _ FILES cannot be found. Why? Supplement: part of... html:
File Upload
Backend php part: ajax. php
The js part is successfully uploaded using js, but two errors occur when jquery is used:
One is to use the $. ajax method:
$(function(){ $(".sub").click(function(){ var fd=new FormData($("#up")); $.ajax({ url: 'ajax.php' , type: 'POST', data: fd, async: false, cache: false, contentType: false, processData: false, success: function (returndata) { alert(returndata); }, error: function (returndata) { alert(returndata); } }); })})
The error message says the pic in $ _ FILES cannot be found. Why?
Supplement: This is done. var fd = new FormData ($ ("# up"); it must be written:
Var fd = new FormData ($ ("# up") [0]);
Specific visible: http://segmentfault.com/q/1010000004213457
And http://segmentfault.com/a/1190000002938709
When using the $. post method:
$.post("ajax.php",fd,function(data){ console.log(data); })
Error: Uncaught TypeError: Illegal invocation
Why?
(Jquery is in version 1.7.1)
Reply content:
Html section:
File Upload
Backend php part: ajax. php
The js part is successfully uploaded using js, but two errors occur when jquery is used:
One is to use the $. ajax method:
$(function(){ $(".sub").click(function(){ var fd=new FormData($("#up")); $.ajax({ url: 'ajax.php' , type: 'POST', data: fd, async: false, cache: false, contentType: false, processData: false, success: function (returndata) { alert(returndata); }, error: function (returndata) { alert(returndata); } }); })})
The error message says the pic in $ _ FILES cannot be found. Why?
Supplement: This is done. var fd = new FormData ($ ("# up"); it must be written:
Var fd = new FormData ($ ("# up") [0]);
Specific visible: http://segmentfault.com/q/1010000004213457
And http://segmentfault.com/a/1190000002938709
When using the $. post method:
$.post("ajax.php",fd,function(data){ console.log(data); })
Error: Uncaught TypeError: Illegal invocation
Why?
(Jquery is in version 1.7.1)
Incorrect settings.
var data = new FormData();data.append('file', $('input[type=file]')[0].files[0]);$.ajax({ url: 'ajax.php', data: data, processData: false, type: 'POST' contentType: 'multipart/form-data', mimeType: 'multipart/form-data', success: function (data) { alert(data); } });
Ajax cannot upload attachments. If you need to upload attachments, see use the jQuery form plug-in.
Ajax can only transmit text streams, but cannot transport binary files.
For more information, see what I wrote about FormData not refreshing uploaded files.
FormData
Ajax cannot upload files, and the enctype attribute of form upload files is required.
contentType: false, processData: false,