func (this *OauthController) LoginByAuth(c *gin.Context) {
fmt.Println(".......................LoginByAuth")
authServer := c.DefaultQuery("authServer", "")
conf = readCredentialFile("\\conf\\creds.json", authServer)
state := randToken()
session := sessions.Default(c)
session.Set("state", state)
session.Save()
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT")
c.Writer.Write([]byte("<html><title>Golang Google</title> <body> <a href='" + getLoginURL(state) + "'><button>Login with Google!</button> </a> </body></html>"))
}
因为有跨域请求问题,所以加了三处header的参数,但是c.Writer.Write 之后,ajax如下:
$.ajax({
url: 'http://localhost:9090/login?authServer=google',
// url: 'http://localhost:9090/login',
type: 'GET',
contentType: 'application/json',
dataType: 'json',
success: function(data){
alert(data)
},
error: function(status){
alert("error:"+ status)
}
});
但是一直走error 分支。
![WeChat Image_20171031143424.png](https://static.studygolang.com/171031/c921d05a5d14767cd32c137dcd8f1b98.png)
后台都是正常的,也没有出现什么问题,是什么原因呢?
有疑问加站长微信联系(非本文作者)