upload file through ajax

前端 未结 1 1775
野性不改
野性不改 2020-12-11 12:00

I use 2 file index.js, upload.php try to upload file(img) through ajax and if success append to div uploadfile_show.
but it doesn\'t work , have few questio

相关标签:
1条回答
  • 2020-12-11 12:27

    FormData takes a form element in its constructor not a jQuery object, also as you are using ajax to submit the form you'll have to prevent its default action(i.e. submitting) by calling preventDefault()

    $('.btn').click(function(e){
        e.preventDefault();
        var uf = $('.uploaddiv form');
        var fd = new FormData(uf[0]);
        $.ajax({
            type: "POST",
            url: "upload.php",
            data: fd,
            processData:false,
            contentType: false,
            success: function(html){
                $('.uploadfile_show').append(html);
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题