The Ajax serialize () form is serialized to upload files and serialize is serialized.

Source: Internet
Author: User

The Ajax serialize () form is serialized to upload files and serialize is serialized.

Upload files using the traditional form submission method

<Form id = "uploadForm" action = "" method = "post" enctype = "multipart/form-data"> <p> upload a file: <input type = "file" name = "file"/> </p> <input type = "submit" value = "Upload"/> </form>

However, the traditional form submission will refresh the page, but in some cases, we do not want the page to be refreshed. In this case, we use Ajax for requests.

Serialize and submit form forms using serialize ()

$.ajax({    url: "",    type: "POST",    data: $('#uploadForm').serialize(),    success: function(data) {      },    error: function(data) {   } });

As shown above, you can use $ ('# uploadForm'). serialize () to serialize the form to pass all parameters in the form to the server.

However, the above method can only pass common parameters. The file stream uploaded to the file cannot be serialized and transmitted. However, mainstream browsers now support an object named FormData. With this object, you can easily upload files using Ajax.

Use FormData for Ajax requests and upload files

<Form id = "uploadForm"> <p> upload a file: <input type = "file" name = "file"/> </p> <input type = "button" value = "upload" onclick = "upload () "/> </form> function upload () {var formData = new FormData ($ (" # uploadForm ") [0]); $. ajax ({url: '', type: 'post', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (data) {}, error: function (data ){}});}

The above section describes how to upload a file using the Ajax serialize () form serialized. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.