Answer the question
In order to leave comments, you need to log in
How to handle a POST request in GO?
I process requests in the north like this:
func main() {
http.HandleFunc("/news", GetNews)
http.ListenAndServe(":8000", nil)
}
func GetNews(w http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
fmt.Println("Work")
}
}
document.querySelector('button').addEventListener('click', () => {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/news', true);
xhr.send();
if (xhr.status != 200) {
console.log( xhr.status + ': ' + xhr.statusText );
} else {
console.log(JSON.parse(xhr.responseText));
}
})
Answer the question
In order to leave comments, you need to log in
In Go, everything is fine, but in js you send an asynchronous request, but process it synchronously, so you cannot get the result, but the request successfully goes to the server and is processed there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question