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!