javascript - jQuery+ajax檔案上傳失敗,什麼原因?

來源:互聯網
上載者:User
html部分:

檔案上傳

後端php部分:ajax.php

js部分,用js上傳的時候是成功的,但用jquery的時候出現了兩種錯誤:
一種是用$.ajax方法:

$(function(){        $(".sub").click(function(){             var fd=new FormData($("#up"));                    $.ajax({            url: 'ajax.php' ,            type: 'POST',            data: fd,            async: false,            cache: false,            contentType: false,           processData: false,            success: function (returndata) {                alert(returndata);            },            error: function (returndata) {                alert(returndata);            }       });              })})  

報錯說找不到$_FILES裡的pic,為什麼呢?
補充:這個地方搞定了,var fd=new FormData($("#up"));需要寫成:
var fd=new FormData($("#up")[0]);
具體可見:http://segmentfault.com/q/1010000004213457
和 http://segmentfault.com/a/1190000002938709

用$.post方法的時候:

$.post("ajax.php",fd,function(data){            console.log(data);         })

報錯:Uncaught TypeError: Illegal invocation
這又是為什麼呢?
(jquery是1.7.1版本的)

回複內容:

html部分:

檔案上傳

後端php部分:ajax.php

js部分,用js上傳的時候是成功的,但用jquery的時候出現了兩種錯誤:
一種是用$.ajax方法:

$(function(){        $(".sub").click(function(){             var fd=new FormData($("#up"));                    $.ajax({            url: 'ajax.php' ,            type: 'POST',            data: fd,            async: false,            cache: false,            contentType: false,           processData: false,            success: function (returndata) {                alert(returndata);            },            error: function (returndata) {                alert(returndata);            }       });              })})  

報錯說找不到$_FILES裡的pic,為什麼呢?
補充:這個地方搞定了,var fd=new FormData($("#up"));需要寫成:
var fd=new FormData($("#up")[0]);
具體可見:http://segmentfault.com/q/1010000004213457
和 http://segmentfault.com/a/1190000002938709

用$.post方法的時候:

$.post("ajax.php",fd,function(data){            console.log(data);         })

報錯:Uncaught TypeError: Illegal invocation
這又是為什麼呢?
(jquery是1.7.1版本的)

設定不對。

var data = new FormData();data.append('file', $('input[type=file]')[0].files[0]);$.ajax({        url: 'ajax.php',        data: data,        processData: false,        type: 'POST'        contentType: 'multipart/form-data',        mimeType: 'multipart/form-data',        success: function (data) {            alert(data);        }    });

Ajax是不能上傳附件的,如果需要上傳附件請參考使用jQuery form外掛程式

ajax只能傳輸文字資料流,不能運輸二進位的檔案。

可參考我寫的關於FormData不重新整理上傳檔案

FormData

ajax不能上傳檔案,還有form上傳檔案的enctype屬性是要有的

      contentType: false,       processData: false,
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.