Hey. There is a task. Can not decide.

  1. Locally rises golang server http: // localhost: 64000 /
  2. At the / login address the user enters the login and password in the form
  3. These data should be sent to the server (there is also GO), for example, at site.ru/login_check using ajax
  4. The server receives data shows me in the console and sends the answer to the local machine, for example, true.

Everything. Those. I have to send a request to the server and get an answer from it without reloading the page.


What am I doing?

1) JS + JQ receive data and send

function test(){ let site = "http://site.ru/login_check"; // Формирую строку для преобразования в JSON user_data = '{ "login" : "'+ $('#login').val() +'", "password" : "'+ $("#password").val() +'"}'; // Преобразую в JSON user_data = JSON.parse(user_data); // Выполняю ajax запрос $.ajax({ url: auth_url, type: 'GET', dataType: 'jsonp', crossDomain: true, success: function(data){ alert(data); }, error: function(){ alert("Ошибка"); } }); } 

2) I am writing code on the GO on the server. I do not receive the data yet, but I'm trying to send some answer.

 func login_check_Handler(w http.ResponseWriter, r *http.Request) { fmt.Print("Вход!") // Дает мне возможность увидеть, что Ajax соединился с сервером (прошел по адресу) // Дальше просто использую пример mapD := map[string]int{"apple": 5, "lettuce": 7} mapB, _ := json.Marshal(mapD) fmt.Fprintf(w, string(mapB)) } PS. import ( "fmt" "encoding/json" "net/http" ) 

3) As a result, an error pops up in the browser

 ncaught SyntaxError: Unexpected token : 

And in the Application tab it is. It turns out that I kind of get something, but what’s wrong I don’t understand. And always get out alert ("Error").

Ps. In other ways, cross-domain queries could not be done. Can you advise what can be done and how to get the data sent from localhost to site.ru in golang.

    1 answer 1

    Instead of cross-domain AJAX, you can send data from the server where the user logs in, make an AJAX request to your own server, where the login form is, and from there we transfer the request to the right place, and we can process the response of the remote server on the server.

    • Really. That is exactly what he did. It turned out much easier. - StaroKep