如何通过此ajax脚本获取文件[重复]

This question already has an answer here:

I'm trying to send a file to a php script through ajax, however I see that it's not being passed properly as I tried to echo that file and got a null response. How can I solve this?

HTML JS code

<script type="text/javascript">
$(function(){
    $("#first").submit(function(event){
        event.preventDefault();

        $(this).find(".error").remove();
        file = $(this).find("input[name=file_save]").val();
        form = $(this);
        url = $(this).attr("action");
        $.post(url,{file_save:file}, function(data){    
            console.log(data);
        },"json");
        return false;
    });
});

</script>

Php script (addImages.php)

<?php
$username = $_POST['files'];
$tableau["error"] = $username;
echo json_encode($tableau);
?>
</div>

$tableau is undefined.
Your PHP srcipt should be like this:

<?php
$username = $_POST['files'];
$tableau = [];
$tableau["error"] = $username;
echo json_encode($tableau);