Send data with ajax | Sololearn: Learn to code for FREE!


Send data with ajax

Hello everyone. How can I post my formdata to server by ajax in javascript? I'mworking with asp mvc

7/30/2020 1:31:22 PM

maryam mirzapoor

2 Answers

New Answer


If you use jQuery, use the ajax method. Specify the data key in your ajax configuration. This will hold your form's parameters which correspond with the named inputs/selects/textareas... from your HTML form. Also, set type to 'POST' to indicate your HTTP request method. contentType should likely be 'application/json'. More detailed explanation of calling an web API using JavaScript is at: Official jquery ajax documentation is at: If you don't already use jQuery and really want to avoid it, fetch can process your HTTP POST request too. More explanation is at:


For example: 'use strict'; function init() { let form = document.getElementById('aForm'); form.addEventListener('submit', (e) => { e.preventDefault(); let formData = new FormData(form); let request = new XMLHttpRequest();'POST', 'aForm', true); request.send(formData); }); } document.addEventListener('DOMContentLoaded', init);