dounai1986 2016-08-29 21:53
浏览 49
已采纳

编组将HTML表单输入到JSON并写入文件golang

This appeared as if it might answer my question, however I am not able to make the output write/save properly to "somefile.json" ( named "deck.json" in my code) no matter what I have tried. My guess is that the form input and the writer are missing each other due to the file being opened/closed at the wrong times, but I honestly do not know. I am attempting to shoehorn this example into a form that allows for a user to input terms and definitions that will save to a JSON file which will be referenced by a JavaScript-powered "flash card" app housed on another page.

Here is the code I have at this moment:

HTML:

<html>
<head>
        <meta charset="utf-8">
        <title>Flashcards for Learning JS</title>
</head>
<body>
   <br>
<form action="/addcard" method="post">
   <input type="text" name="term" placeholder="Term">
   <br>
   <br>
   <input type="text" name="definition" placeholder="Definition">
   <br>
   <br>
   <input type="submit" value="Add Card">
</form>
</body>
</html>

GO:

 package main

import (
    "encoding/json"
    "html/template"
    "net/http"
    "os"
)

type Card struct {
    Term       string `json:"term"`
    Definition string `json:"definition"`
}

func open(w http.ResponseWriter, r *http.Request) {
    t, _ := template.ParseFiles("Form.html")
    t.Execute(w, nil)
}

func addcard(w http.ResponseWriter, r *http.Request) {
    f, err := os.Open("deck.json")
    if err != nil {
        http.Error(w, err.Error(), 500)
        return
    }

    card := new(Card)
    card.Term = r.FormValue("term")
    card.Definition = r.FormValue("definition")

    b, err := json.Marshal(card)
    if err != nil {
        http.Error(w, err.Error(), 500)
        return
    }

    f.Write(b)
    f.Close()
}

func main() {
    http.HandleFunc("/", open)
    http.ListenAndServe(":8080", nil)
    http.HandleFunc("/addcard", addcard)
}
  • 写回答

1条回答 默认 最新

  • dongyoufo5672 2016-08-31 18:45
    关注

    Changed per recommendations made by @JimB. Fixed handler order, and program successfully concatenates each successive submission as valid JSON to the file in question.

    HTML:

    <html>
    <head>
            <meta charset="utf-8">
            <title>Flashcards for Learning JS</title>
    </head>
    <body>
       <br>
    <form action="/addcard" method="post">
       <input type="text" name="term" placeholder="Term">
       <br>
       <br>
       <input type="text" name="definition" placeholder="Definition">
       <br>
       <br>
       <input type="submit" value="Add Card">
    </form>
    </body>
    </html>
    

    GO:

    package main
    
    import (
        "encoding/json"
        "html/template"
        "net/http"
        "os"
    )
    
    type Card struct { //defining the data structure for our virtual flashcards
        Term       string `json:"Term"`
        Definition string `json:"Definition"`
    }
    
    func open(w http.ResponseWriter, r *http.Request) {
        t, _ := template.ParseFiles("addcard.html")
        t.Execute(w, nil)
    }
    
    func addcard(w http.ResponseWriter, r *http.Request) {
        f, err := os.OpenFile("deck.json", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
        if err != nil {
            http.Error(w, err.Error(), 500)
            return
        }
        defer f.Close()
    
        card := new(Card)
        card.Term = r.FormValue("term")
        card.Definition = r.FormValue("definition")
    
        b, err := json.Marshal(card)
        if err != nil {
            http.Error(w, err.Error(), 500)
            return
        }
    
        f.Write(b)
        f.Close()
    }
    
    func main() {
        http.HandleFunc("/addcard", addcard)
        http.HandleFunc("/", open)
        http.ListenAndServe(":8080", nil)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站