doufuxi7093 2018-03-23 07:36
浏览 60

在Golang中使用Ajax将JavaScript变量发布到mysql

I find screen size using javascript and want this data to post mysql using ajax. But this is not working. How do I define screen size variable in new.go file? How do I post automatically created screen size information to mysql using ajax?

Here is new.html code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Call Go</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script type="text/javascript">


            (function (window) {


                    // screen
                    var screenSize = '';
                    if (screen.width) {
                        width = (screen.width) ? screen.width : '';
                        height = (screen.height) ? screen.height : '';
                        screenSize += '' + width + " x " + height;
                    }


                window.jscd = {
                    screen: screenSize,
                };
            }(this));
            alert(
                'Screen Size: ' + jscd.screen
            );


                    $("#callGo").on('click', function() {
                    $.ajax({
                        url: "http://localhost:9000/callme",
                        method: "POST",
                        success: function() {
                            $("#response").html(jscd.screen);
                        },
                    });
                });

        </script>
    </head>
    <body>
        <button id="callGo" type="submit">Call Go Code</button>
        <div id="response"></div>
    </body>
</html>

When I click callGo named button then response named div success added text must be shown. But how do I do that? How do I connect ajax sending data to go backend code to insert mysql?

Here is my new.go file

package main

import (
    "database/sql"
    "html/template"
    "log"
    "net/http"

    _ "github.com/go-sql-driver/mysql"
)

// Load the index.html template.
var tmpl = template.Must(template.New("tmpl").ParseFiles("new.html"))

func main() {
    // Serve / with the index.html file.
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        if err := tmpl.ExecuteTemplate(w, "new.html", nil); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
    })

    // Serve /callme with a text response.
    http.HandleFunc("/callme", func(w http.ResponseWriter, r *http.Request) {

        db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/test_db")
        defer db.Close()
        if err != nil {
            panic(err.Error())
        }
        if r.Method == "POST" {
            insForm, err := db.Prepare("INSERT INTO screen(screen_size) VALUES(?)")
            if err != nil {
                panic(err.Error())
            }
            insForm.Exec(jscd.screen)

        }

        http.Redirect(w, r, "/", 301)
    })

    // Start the server at http://localhost:9000
    log.Fatal(http.ListenAndServe(":9000", nil))
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
    • ¥15 Attention is all you need 的代码运行
    • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
    • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
    • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
    • ¥30 关于<main>标签页面跳转的问题
    • ¥80 部署运行web自动化项目
    • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
    • ¥30 VMware 云桌面水印如何添加