i'm currently trying to send variables (in this case a struct array into the JS variable Data) to my website. In Addition i want to read a String out of my TextField and trigger a Function with it as parameter. My question is, how to get/recieve/send the variable from Go/Javascript? For now it doesnt even call the fillChoiceBox() function. I set it as onClick Function but nothing happens. A Code Example would be very nice.
Here my Golang code:
//Klimakammer struct
type klimakammer struct {
Name string `json:"name"`
Hersteller string `json:"hersteller"`
IP string `json:"ip"`
SollTemp string `json:"solltemp"`
IstTemp string `json:"isttemp"`
SollFcht string `json:"sollfcht"`
IstFcht string `json:"istfcht"`
kammerstart bool
kammerstop bool
}
//Ct01 Klimakammern erstellen
var Ct01 = klimakammer{"ct01", "weiss", "10.0.62.22", "", "", "", "", false, true}
//Kammern - Fill Klimakammer Array
var Kammern = []klimakammer{
Ct01,
}
func main() {
fs := http.FileServer(http.Dir("./static/"))
http.Handle("/", fs)
http.ListenAndServe(":8080", nil)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
buff, _ := json.Marshal(&Kammern)
fmt.Fprintln(w, string(buff))
})
}
and my JS-Code:
function fillChoiceBox() {
**//That was my first try (But it never found the URL "/getKammer"):**
// $.ajax({
// url: "http://localhost:8080/getKammer",
// method: "GET",
// function(data) {
// var $dropdown = $("#Kammer");
// for( i=0; i <= data.length; i++) {
// $dropdown.append($("<option />").val(data[i]).text(this.name));
// $("#getKammer").prop('disabled', true);
// }
// },
// })
var i;
var $dropdown = $("#Kammer");
for( i=0; i <= data.length; i++) {
$dropdown.append($("<option />").val(data[i]));
}
$("#getKammer").prop('disabled', true);
}
HandleFuncafterListenAndServemakes no sense.HandleFunc/Handlemore than once with the same pattern will panic (currently your code doesn't panic because the second call is not reached thanks toListenAndServe)."/"is not a requirement, you can substitute it with whatever else (within reason).