Use jQuery. ajax to upload forms based on XMLHttpRequest and FormData

Source: Internet
Author: User

XMLHttpRequest has been launched for a long time, and W3C has introduced some good features. Ruan Yifeng has a good article on this, which summarizes the XMLHttpRequest Level 2 user guide. Excluding outdated browsers in actual use, the simplest way to use is to generate the submit event of the FORM hander FORM using the standard HTML syntax, generate XMLHttpRequest with FormData to submit data, return false to submit to process XMLHttpRequest results, and form submission in ajax mode is completed. Of course, this supports file upload. If we use jQuery. the ajax code is like this. Js code: $ ('yourform '). submit (function () {var formdata = new FormData (this); $. ajax ({type: 'post', url: '/yourpath', data: formdata,/*** must be false to automatically add the correct Content-Type */contentType: false,/*** must be false to avoid jQuery's default processing of formdata * XMLHttpRequest will correctly process formdata */processData: false }). then (function () {// doneCal}, function () {// failCal}); return false ;}); that's simple under standard. In many cases, users are required to use modern browsers. For example, for editing, it corresponds to a customized enterprise user.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.