Home > Web Front-end > JS Tutorial > body text

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

不言
Release: 2019-01-10 10:14:43
forward
7341 people have browsed it

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)
    }
  })
})
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!