前端代码
<script type="text/javascript">
var filename=$("input[name=filename]").val();
var crowd_file =$('#crowd_file')[0].files[0];
var formData =new FormData();
formData.append("crowd_file",crowd_file);
formData.append("filename", filename);
$.ajax({
url:'http://****/test/index.php?type=bb',
dataType:'json',
type:'POST',
async:false,
data: formData,
processData:false, // jQuery不要去处理发送的数据
contentType:false, // jQuery不要去设置Content-Type请求头
success:function(data){
console.log(data);
// if (data.status == 'ok') {
// alert('上传成功!');
// }
},
error:function(response){
console.log(response);
}
});
</script>
formData 传值后台,接收方法。
<?php
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:OPTIONS, GET, POST'); // 允许option,get,post请求
header('Access-Control-Allow-Headers:x-requested-with'); // 允许x-requested-with请求头
header('Content-type: application/json');
header("Content-type:text/html;charset=utf-8");
$data = $_POST;
$data['files'] = $_FILES;
echo $data;
?>
网友评论