In jQuery, we have $('form').serialize()
to post all the form data in a single line. Is there any method available in JavaScript to post form data like this or is there an alternative in JavaScript for the code below code?
var url = "path.php";
$.ajax({
type: "POST",
url: url,
data: $("#idForm").serialize(), //like this i need in js
success: function(data)
{
}
});
You can use FormData, like this:
let data = new FormData(document.querySelector('form'));
And then:
let request = new XMLHttpRequest();
request.open("POST", "url");
request.send(data);
More info: FormData API
I hope it helps you
Serialize is creating a parameter string like field1=value1&field2=value2
When using the jquery ajax, the data can provided as object or array
var x = document.getElementById("myForm");
var formData = [];
var i;
for (i = 0; i < x.length; i++) {
formData[x.elements[i].name] = x.elements[i].value;
}
$.ajax({
type: "POST",
url: url,
data: formData
success: function(data)
{
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.