[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 (
"net/http"
"github.com/gin-gonic/contrib/static"
"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() {
// Set the router as the default one shipped with Gin
router := gin.Default()
router.Use(Cors())
// 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
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
//PATRZ: documentation/couchdb/local.ini; documentation/couchdb/corsy_ustawienie.txt
c.Header("Content-Type", "application/json")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
fmt.Println("------------------------------------------------------------------")
var newCard Card
c.Bind(&newCard)

View File

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