普通网友 2017-02-16 13:22
浏览 46

如何通过此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>
  • 写回答

1条回答 默认 最新

  • dongliechuich10319 2017-02-16 13:24
    关注

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

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

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统