我试图从olanod answer重新创建本指南,但不适用于我。上传带有AJAX文件的文件
我想用AJAX上传文件,但我得到一个空的$ _ POST:
<form enctype="multipart/form-data">
<input type="file" name="file">
<br>
<input type="text" name="as" value="asd">
<!--<button type='button' class="add_more">Add More Files</button>-->
<input type="button" value="Upload" />
</form>
,这是我的脚本(从olanod answer复制粘贴):
<script>
$(document).ready(function(){
/* $('.add_more').click(function(e){
e.preventDefault();
$(this).before("<input name='upfile[]' type='file'/><br>");
});*/
$(':button').on('click', function()
{
$.ajax({
// Your server script to process the upload
url: 'ajax.php',
type: 'POST',
// Form data
data: new FormData($('form')[0]),
// Tell jQuery not to process data or worry about content-type
// You *must* include these options!
cache: false,
contentType: false,
processData: false,
// Custom XMLHttpRequest
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
// For handling the progress of the upload
myXhr.upload.addEventListener('progress', function(e) {
if (e.lengthComputable) {
$('progress').attr({
value: e.loaded,
max: e.total,
});
}
} , false);
}
return myXhr;
},
});
});
});
</script>
正如我说,我想看看我要采取什么,这是我的PHP文件的结果:
array(1){ [“as”] => 串(3) “ASD” }
我返回一个文本字段是肯定的。
P.D:对不起,我的英语。我希望你能理解我,我正在努力!
你应该使用'$ _FILES'而不是'$ _POST' – C2486
@ user2486噢,我的上帝,我觉得很蠢!感谢m8,你是对的! –