zoukankan      html  css  js  c++  java
  • gin cors

    ...

    package main
    
    import (
    	"github.com/gin-gonic/gin"
    )
    
    func pingHandle(c *gin.Context) {
    	c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
    	c.Header("Access-Control-Allow-Origin", "*")        // 这是允许访问所有域
    	c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")      //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
    	//  header的类型
    	c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,X_Requested_With,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma")
    	//              允许跨域设置                                                                                                      可以返回其他子段
    	c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar")      // 跨域关键设置 让浏览器可以解析
    	c.Header("Access-Control-Max-Age", "172800")        // 缓存请求信息 单位为秒
    	c.Header("Access-Control-Allow-Credentials", "false")       //  跨域请求是否需要带cookie信息 默认设置为true
    	c.Set("content-type", "application/json")       // 设置返回格式是json
    
    	std := []Stu{{"11","name"},{"12","huzh"}}
    	//jsonData := []byte(`[{"id":"11","name":"huzh"}]`)
    	//c.Data(http.StatusOK,"application/json",jsonData)
    	//c.DefaultQuery("name")
    	c.JSON(200, gin.H{
    		"datakey":std ,
    	})
    }
    
    
    //文件可能很大,gin默认内存最大文件是32M,超出部分则流形式一点点读,也可以设置
    // router.MaxMultipartMemory = 8 << 20 // 8 MiB
    type Stu struct {
    	Id string `json:"id"`
    	Name string `json:"name"`
    }
    func main() {
    
    	r := gin.Default()
    	r.GET("ping", pingHandle)
    	r.Run(":9800") //默认不传递则是9090
    }
    

      

  • 相关阅读:
    linux下面安装maven
    go 安装
    linux scp 服务器远程拷贝
    git有merge时如何删除分支
    influxdb ERR: error parsing query: found -, expected
    influxDB学习总结
    influxdb Measurements
    go exec: "gcc": executable file not found in %PATH%
    php计算脚本执行时间
    安装nodejs和grunt以后出现 /usr/bin/env: node: No such file or directory
  • 原文地址:https://www.cnblogs.com/eiguleo/p/14586927.html
Copyright © 2011-2022 走看看