使用jQuery + Ajax + PHP 实现无刷新上传

颜霖
2023-12-01
<form id = "frm" >
    <div id = "box2" >
        <input type = "file" name = "file1" id = "file1" />
        <input type = "button" value = "上传" id = "upload" />
    </div>
</form>
$ ( "#upload" ). click ( function (){
    var formData = new FormData ( $ ( "#frm" )[ 0 ]);
    $ . ajax ({
        url : "https://blog.454142.com/ajaximg.php" ,
        type : 'POST' ,
        data : formData ,
        async : false ,
        cache : false ,
        contentType : false ,
        processData : false ,
        success : function ( res ) {
            alert ( res . str )
        },
        error : function ( res ) {
            alert ( res . str );
        }
    });
})
 类似资料: