How to display a message on the DOM and redirect to a website at the same time in Go?

huangapple go评论70阅读模式
英文:

How to display a message on the DOM and redirect to a website at the same time in Go?

问题

我正在尝试在DOM上显示一条消息,然后将用户重定向到一个网站,但只有一个会起作用。

相关代码:

func main() {
    path, _ := filepath.Abs(os.Args[1])
    app := App{
        Auth:         somePackage.someMethod(许多参数),
        LibPath: path,
    }
    url := app.Auth.AuthURL(state)
    browser.OpenURL(url)

    mux := http.NewServeMux()
    mux.HandleFunc("/callback", app.CallbackHandler)
    if err := http.ListenAndServe("localhost:3000", mux); err != nil {
        log.Fatalf("无法启动服务器:%v", err)
    }
}


func (app *App) CallbackHandler(w http.ResponseWriter, r *http.Request) {
    token, err := app.Auth.Token(state, r)
    if err != nil {
        log.Print(err)
        http.Error(w, "无法获取令牌", http.StatusNotFound)
        return
    }

    app.Client = app.Auth.NewClient(token)

    app.someFunc()

    // w.Write([]byte("successful, Please check your dashboard"))
    http.Redirect(w, r, "https://someweb.site/dashboard/", http.StatusSeeOther)

    log.Print("完成")
}

只有一个会起作用,要么是w.Write([]byte("successful, Please check your dashboard")),要么是http.Redirect(w, r, "https://someweb.site/dashboard/", http.StatusSeeOther)

当两者同时使用时,页面(localhost:3000:....)显示:

successful, Please check your dashboard !a href="https://someweb.site/dashboard/">See Other</a!(我在markdown中使用!来显示它)

应用程序的功能没有错误,按预期运行并完成工作。

问题是什么,如何解决?

英文:

I'm trying to display a message on the DOM and then redirect the user to a website, but only one would work.

Relevant code:

func main() {
	path, _ := filepath.Abs(os.Args[1])
	app := App{
		Auth:         somePackage.someMethod(many params),
		LibPath: path,
	}
	url := app.Auth.AuthURL(state)
	browser.OpenURL(url)

	mux := http.NewServeMux()
	mux.HandleFunc(&quot;/callback&quot;, app.CallbackHandler)
	if err := http.ListenAndServe(&quot;localhost:3000&quot;, mux); err != nil {
		log.Fatalf(&quot;Failed to start the server: %v&quot;, err)
	}
}


func (app *App) CallbackHandler(w http.ResponseWriter, r *http.Request) {
	token, err := app.Auth.Token(state, r)
	if err != nil {
		log.Print(err)
		http.Error(w, &quot;Couldn&#39;t get token&quot;, http.StatusNotFound)
		return
	}

	app.Client = app.Auth.NewClient(token)

	app.someFunc()

	// w.Write([]byte(&quot;successful, Please check your dashboard&quot;))
	http.Redirect(w, r, &quot;https://someweb.site/dashboard/&quot;, http.StatusSeeOther)

	log.Print(&quot;done&quot;)
}

only one thing works, either w.Write([]byte(&quot;successful, Please check your dashboard&quot;)) or http.Redirect(w, r, &quot;https://someweb.site/dashboard/&quot;, http.StatusSeeOther).

When both are used, the page (localhost:3000:....) shows:

successful, Please check your dashboard !a href="https://someweb.site/dashboard/">See Other</a! (i put ! to show it in markdown)

There are no errors in the functioning of the application, runs as it is expected to and does the job.

What is the issue and how to solve it?

答案1

得分: 1

你不能指示浏览器同时转到另一个网站并显示先前的网站。浏览器只能一次执行这些操作。因此,通常情况下,HTML内容会被显示出来,然后通过一些定时器在短时间后自动转到新的网站。这可以使用JavaScript实现,也可以使用简单的meta标签实现:

<meta http-equiv="refresh" content="5;url=https://example.com/" />
这段内容将在5秒钟后重定向到example.com。
英文:

You cannot instruct the browser to go to another site and at the same time display the previous site. A browser can do these things only one after the other. So typically the HTML content is displayed and through some timer it will automatically move to the new site after a short time. This could be implemented with Javascript but also with a simple meta tag:

&lt;meta http-equiv=&quot;refresh&quot; content=&quot;5;url=https://example.com/&quot; /&gt;
This content is displayed for 5 seconds, before it will redirect to example.com

huangapple
  • 本文由 发表于 2023年1月15日 15:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75123662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定