Attempting to extract data using FormData
AJAX request on the JavaScript side
function sendForm()
{
let form=document.getElementById("myForm");
var formData = new FormData();
for(var i=0; i<form.length; i++)
{
formData.append(form[i].name, form[i].value);
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
console.log(xmlHttp.responseText)
}
}
xmlHttp.open("post", url);
xmlHttp.setRequestHeader("Content-Type", "multipart/form-data");
xmlHttp.send(formData);
}
Handling in Go side
func login(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username:= r.FormValue("username") // Data from the form
password:= r.FormValue("password")
fmt.Println(username,password) //getting empty
}
Even though I tried using the form-data option in Postman, I encountered the same issue. However, it works fine in PHP. In Go lang, I am unsure how to handle multipart/form-data.