Send data with ajax | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Send data with ajax

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

30th Jul 2020, 1:31 PM
maryam mirzapoor
maryam mirzapoor - avatar
2 Answers
+ 2
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:
30th Jul 2020, 3:10 PM
Josh Greig
Josh Greig - avatar
+ 1
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);
30th Jul 2020, 3:10 PM
JaScript - avatar