• Go post重定向的两种方式


    1、Location
    2、http.Redirect

    代码

    /index -> /login -> /home

    package main
    import (
    	"fmt"
    	"log"
    	"net/http"
    	"strings"
    )
    
    
    func index(w http.ResponseWriter, r *http.Request) {
    	flag := true
    	if strings.Contains("/index", r.URL.Path){
    		flag = false
    	}
    	fmt.Println(flag)
    	w.Header().Set("Content-Type", "text/html")
    	w.Write(indexHTML)
    }
    
    var indexHTML = []byte(`
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <button class="button">发送POST请求,登录</button>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
        $(".button").click(
            function (){
                $.post("http://127.0.0.1:9090/login", {}, function (res){
                    console.log(res)
                })
            }
        )
    
    </script>
    
    </body>
    </html>
    `)
    
    func login(w http.ResponseWriter, r *http.Request) {
    	// way 1
    	fmt.Print(fmt.Sprintf("%v \n", r))
    	w.Header().Set("Cache-Control", "must-revalidate, no-store")
    	w.Header().Set("Content-Type", " text/html;charset=UTF-8")
    	w.Header().Set("Location", "http://127.0.0.1:9090/home")//跳转地址设置
    	w.Header().Set("Access-Control-Allow-Origin", "*")
    	w.Header().Set("Access-Control-Allow-Credentials", "true")
    	w.Header().Set("Access-Control-Allow-Method", "GET,OPTION,POST")
    	w.Header().Set("Access-Control-Allow-Headers", "http://127.0.0.1:9090/home")
    	w.WriteHeader(301)//关键在这里!
    	// way 2
    	// http.Redirect(w, r, "/home", 301)
    
    }
    
    func home(w http.ResponseWriter, r *http.Request) {
    	w.Header().Set("Content-Type", "text/html")
    	w.Write(homeHTML)
    }
    
    var homeHTML = []byte(`
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <button class="button">登录成功!</button>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
        $(".button").click(
            function (){
                $.post("http://127.0.0.1:9090/login", {}, function (res){
                    console.log("success !")
                })
            }
        )
    
    </script>
    
    </body>
    </html>
    `)
    
    
    func main() {
    	http.HandleFunc("/index", index)
    	http.HandleFunc("/login", login)
    	http.HandleFunc("/home", home)
    	err := http.ListenAndServe(":9090", nil) //设置监听的端口
    	if err != nil {
    		log.Fatal("ListenAndServe: ", err)
    	}
    }
    
    
  • 相关阅读:
    散列点斜率算法解剖
    c# 3d图像显示
    .net core 运行不需命令行
    c# 异步调用(异步模式,基于事件)
    分配在堆上还是分配在栈上及其区别
    c# 自定义按钮,渐变颜色(含中心向四周渐变,单方向渐变)
    玩转Web值jquery(一)---对表单中的某一标签批量处理(以input为例)
    设计模式 模版方法模式 展现程序员的一天
    【Android进阶】Android面试题目整理与讲解(二)
    【Android进阶】让程序运行效率更高的编程技巧总结
  • 原文地址:https://www.cnblogs.com/taozhengquan/p/16221388.html
Copyright © 2020-2023  润新知