Home >Web Front-end >JS Tutorial >Ajax error when uploading files/photos TypeError: Illegal invocation solution

Ajax error when uploading files/photos TypeError: Illegal invocation solution

不言
不言forward
2019-01-10 10:14:437596browse

The content of this article is about the solution to the TypeError: Illegal invocation error when Ajax uploads files/photos. It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. helped.

Problem

Ajax error when uploading files/photos TypeError: Illegal invocation

Ajax error when uploading files/photos TypeError: Illegal invocation solution

Solution

Online search problems, the error reasons may be as follows, check in order:

  1. The request type is wrong, such as post request, But what is set in the background is that the get request

  2. parameters are incorrect. If no parameters are passed, or the parameters do not correspond

  3. File type parameters are pre-processed

After checking, it is found that it should Reason 3, so modify the code and set processData of $.ajax: 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)
    }
  })
})

The above is the detailed content of Ajax error when uploading files/photos TypeError: Illegal invocation solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete