[CORS-POLICY] DZIAŁA!

This commit is contained in:
pawlaczyk 2019-01-09 09:22:07 +01:00
parent bf48af50c6
commit 617fb64dbe
3 changed files with 49 additions and 13 deletions

View File

@ -6,16 +6,47 @@ package main
import ( import (
"net/http" "net/http"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func Cors() gin.HandlerFunc {
//https://razil.cc/post/2018/10/go-cors-request-on-gin/
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin")
if origin != "" {
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization") //自定义 Header
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
}
if method == "OPTIONS" {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization") //自定义 Header
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
c.AbortWithStatus(http.StatusNoContent)
}
c.Next()
}
}
func main() { func main() {
// Set the router as the default one shipped with Gin // Set the router as the default one shipped with Gin
router := gin.Default() router := gin.Default()
router.Use(Cors())
// Serve frontend static files // Serve frontend static files
router.Use(static.Serve("/", static.LocalFile("./views", true))) // router.Use(static.Serve("/", static.LocalFile("./views", true)))
// config.AllowOrigins = []string{"http://localhost:8080/"}
// Setup route group for the API // Setup route group for the API
api := router.Group("/api") api := router.Group("/api")

View File

@ -147,6 +147,13 @@ func addNewCardView(c *gin.Context) {
//ale dodawanei tutaj bo i tak mozna to zrobic w tej samem domenie co couchdb //ale dodawanei tutaj bo i tak mozna to zrobic w tej samem domenie co couchdb
//PATRZ: documentation/couchdb/local.ini; documentation/couchdb/corsy_ustawienie.txt //PATRZ: documentation/couchdb/local.ini; documentation/couchdb/corsy_ustawienie.txt
c.Header("Content-Type", "application/json") c.Header("Content-Type", "application/json")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
var newCard Card var newCard Card
c.Bind(&newCard) c.Bind(&newCard)

View File

@ -32,6 +32,8 @@
<script> <script>
import format from 'date-fns/format' import format from 'date-fns/format'
import axios from 'axios'
export default { export default {
data(){ data(){
return{ return{
@ -78,18 +80,14 @@ export default {
cardObj.blank = blank cardObj.blank = blank
cardObj.text = this.cardText cardObj.text = this.cardText
axios.get("http://127.0.0.1:3000/api/getUsersView" , { crossdomain: true }
axios.get(url_status, {
headers: {
'Content-Type': 'application/json',
}
}
).then(function(response){ ).then(function(response){
vm.friendlist = response.data; console.log(response)
}) })
.catch(function(error){ .catch(function(error){
vm.friendlist = "An error occured: " + error; console.log("ERROR: ", error);
}); });
this.$emit('cardAdded') this.$emit('cardAdded')
} }
} }