[英]json: cannot unmarshal string into Go value of type main.test_struct
[英]JSON cannot unmarshal object into GO value of type string
使用github.com/zserge/lorca
包編寫Golang應用程序。 這會將golang函數綁定到Javascript。 我有帶有文本輸入和提交按鈕的HTML,該按鈕應將文本輸入作為arg傳遞到Javascript綁定中。 它看起來如下:
<input type="text" name="MACADD" style="height:20px; width:210px">
<input type="submit" value="submit" onclick="JSBINDFUNC(MACADD)">
JSBINDFUNC
采用golang類型的string
作為輸入。 當我點擊提交時,它應該將為MACADD
輸入的文本作為一個參數傳遞給JSBINDFUNC
函數。
但是,我回來了
exception":{"type":"string","value":"json: cannot unmarshal object into Go value of type string"}
需要這個object
成為golang string
。
更完整的截圖:
package main
import (
"fmt"
"log"
"net/url"
"github.com/zserge/lorca"
)
func main() {
ui, err := lorca.New("data:text/html,"+url.PathEscape(`
<html>
<form action="/action_page.php">
MAC Address:<br>
<input type="text" name="MACADD" style="height:20px; width:210px">
<input type="submit" value="Submit" onclick="JSBINDFUNC(MACADD)">
</form>
</body>
</html>
`), "", 480, 320)
if err != nil {
log.Fatal(err)
}
//ui.Bind implemented @ https://github.com/zserge/lorca/blob/master/ui.go#L110
ui.Bind("JSBINDFUNC", func(MAC string) {
fmt.Println(MAC)
return
})
defer ui.Close()
<-ui.Done()
}
問題出在您的JavaScript。 像這樣更新您的onclick屬性:
<input type="submit" value="Submit" onclick="JSBINDFUNC(MACADD.value)">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.