Home php教程 PHP开发 Problems encountered when using jQuery.ajax to upload forms with files

Problems encountered when using jQuery.ajax to upload forms with files

Dec 08, 2016 pm 01:33 PM

When I was helping someone read the code today, I encountered a small problem. There are some issues when using jQuery to upload a form with a file.

First of all, because FormData is used, processData: false must be configured in the parameters passed in $.ajax.

Otherwise, an Illegal invocation exception will be thrown, because jQuery will process the data in the incoming data field by default.

The official document explains this:

Problems encountered when using jQuery.ajax to upload forms with files

Secondly, pay attention to the Content-Type header of the request. The default is application/x-www-form-urlencoded; charset=UTF-8, which is what we usually see. "a=A&b=B" format. But when using FormData, this doesn't work.

Add the contentType field to the parameter and set its value to false. If jQuery version is less than 1.6, manually set to multipart/form-data. For specific instructions, please see the documentation:

Problems encountered when using jQuery.ajax to upload forms with files

I usually use native XMLHttpRequest before, so I have never encountered this problem. Now that you have encountered it, you must solve it. So record it for future reference.

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 admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)