M
M
Michael2017-02-26 12:44:58
JavaScript
Michael, 2017-02-26 12:44:58

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")
  }
}

And send like this:
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)); 
                }
            })

AJAX goes to the server, but for some reason it is not processed there. How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-02-27
@mak_ufo

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 question

Ask a Question

731 491 924 answers to any question