V
V
Vadim Rublev2020-09-23 14:08:42
JavaScript
Vadim Rublev, 2020-09-23 14:08:42

How to pass in the response of the Go-server the correct string format for the HTML code?

Here it works fine:

<div id="id_divInner">test div top</div>
<div id="id_divOuter">testDiv_bottom</div>
<button onmouseover="divTopInner()">Ok_test_inner</button>
<button onmouseover="divBottomOuter()">Ok_TestOuter</button>
<script>
    function divTopInner() {
    document.querySelector('#id_divInner').innerHTML = "<p>test_P_<br>новая строка</p>";   // test_P_ □новая строка
  }
              
  function divBottomOuter() {								
    document.querySelector('#id_divOuter').outerHTML = 'Здесь <a href="#">ссылка-A</a> Outer';   // Здесь ссылка-A Outer
  }
</script>


And the same with the server response is displayed "as text":

JS:
...
xhr.onloadend = function() {
    if (xhr.status === 200) {					
    document.querySelector('#id_divInner').innerHTML = xhr.response;   // Тест-ответ сервера со <a href="#">ссылкой-a</a>.<br>Следующая строка.
  }
}


go:
http.HandleFunc("/rout", func(w http.ResponseWriter, r *http.Request) {
    ...
    var dataResponRegNo = "Тест-ответ сервера со <a href="#">ссылкой-a</a>.<br>Следующая строка."
    var buttonRegs, _ = template.New("dataResp").Parse("{{ .}}")
    buttonRegs.Execute(w, dataResponRegNo)
    return
}


Is this a problem in Go or in JS? How can you decide?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question