天天看點

ajax檔案typeerror,Ajax上傳檔案/照片時報錯TypeError :Illegal invocation的解決方法

ajax檔案typeerror,Ajax上傳檔案/照片時報錯TypeError :Illegal invocation的解決方法

本篇文章給大家帶來的内容是關于Ajax上傳檔案/照片時報錯TypeError :Illegal invocation的解決方法,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

問題

Ajax上傳檔案/照片時報錯TypeError :Illegal invocation

ajax檔案typeerror,Ajax上傳檔案/照片時報錯TypeError :Illegal invocation的解決方法

解決

網上搜尋問題,錯誤原因可能有以下幾個,依次檢查:

請求類型有誤,如post請求,但在背景設定的是get請求

參數有誤。 如沒有傳參,或是參數對應不上去

File類型的參數被預先處理了

檢查後發現應該時原因3,故修改代碼,設定$.ajax的processData: false:

getToken().then( res => {

console.log('擷取七牛雲token後上傳圖檔')

if(!res.hasOwnProperty('data')) return

// 整理參數

var formData = new FormData()

formData.append('token', res.data)

formData.append('file', file)

$.ajax({

url: '',

type: 'POST',

contentType: 'multipart/form-data',

processData: false, // 增加這一行,不處理參數

data: formData,

success: function (result) {

console.log(result)

}

})

})