S
S
starosta462018-12-12 12:48:07
JavaScript
starosta46, 2018-12-12 12:48:07

Why doesn't the golang server send a response?

There is a golang server:

package main

import (
  "fmt"
  "net/http"
)

var i = 0

func receiveSend(w http.ResponseWriter, r *http.Request) {
  if r.Method == "POST" {
    sendedData := r.FormValue("sendedData")
    fmt.Println("My request is: ", sendedData)
    fmt.Fprintln(w,"sd")
  }
}

func main() {

  http.HandleFunc("/", receiveSend)

  http.ListenAndServe(":8000", nil)
}


And the page:
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="  crossorigin="anonymous"></script>


<body>
    <button id="send">Send</button>
    <p class="results">Ждем ответа</p>
  </body>
  

<script>
  var data;
  $('#send').click(function() {
    $.ajax({
      url: "http://localhost:8000",
      method: "POST",
      data : { sendedData: 'Hello'},
            success : function(data) {
                $('.results').html(data);
            },
    });
  });
  
</script>
</html>


The request to the server leaves, the server receives and outputs data, but does not send anything in response. With what it can be connected and where to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vman, 2018-12-12
@starosta46

I think the problem is in CORS

func receiveSend(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Access-Control-Allow-Origin", "*")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question