簡體   English   中英

JSON無法將對象解組為字符串類型的GO值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM