【问题标题】:JSON cannot unmarshal object into GO value of type stringJSON 无法将对象解组为字符串类型的 GO 值
【发布时间】:2019-03-28 04:10:44
【问题描述】:

使用 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()
}

【问题讨论】:

  • 您需要发布您的 go-code sn-p。不熟悉这个 pkg,我怀疑有人将表单数据编组到一个结构中——因为表单有很多字段——并且编组为单个字符串是行不通的。
  • @colminator 刚刚更新,您应该能够复制/粘贴 go build 并得到确切的错误。确保您已安装 Chrome。
  • html 应该类似于MACADD.value 吗?
  • @atayenel 天才。谢谢。

标签: javascript html go


【解决方案1】:

问题在于您的 javascript。像这样更新你的 onclick 属性:

<input type="submit" value="Submit" onclick="JSBINDFUNC(MACADD.value)">

【讨论】:

  • 我刚刚根据@atayenel 将JSBINDFUNC(MACADD) 更改为JSBINDFUNC(MACADD.value)。这行得通。
  • 我刚刚编辑了答案以反映@atayenel 更短的建议
  • 我喜欢洛卡。我很高兴这是 html 问题而不是 lorca 问题 :)
猜你喜欢
  • 2021-03-05
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 2018-08-19
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多