簡單的jquery ajax文件上傳功能

/* * 圖片上傳 * 注意若是不加processData:false和contentType:false會報錯 */
    function uploadImage(image) {
        var imageForm = new FormData();
        imageForm.append("upfile", $("#file").get(0).files[0]);
        var url = url;
        $.ajax({
            type: 'POST',
            url: url,
            data: imageForm,
            processData: false,  // 告訴jQuery不要去處理髮送的數據
            contentType: false,   // 告訴jQuery不要去設置Content-Type請求頭
            success: function (data) {
                var jsonData = jQuery.parseJSON(data)
                console.log(jsonData.url);
            },
            error: function () {
            }
        });
    }
相關文章
相關標籤/搜索