Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go语言解决浏览器跨域问题 #37

Open
wuyongxiu opened this issue Oct 29, 2017 · 2 comments
Open

go语言解决浏览器跨域问题 #37

wuyongxiu opened this issue Oct 29, 2017 · 2 comments

Comments

@wuyongxiu
Copy link
Owner

wuyongxiu commented Oct 29, 2017

1 跨域简介

跨域产生的原因,什么是跨域,如何解决跨域,可以参考阮一峰写的博文:http://www.ruanyifeng.com/blog/2016/04/cors.html

2 chrome浏览器模拟跨域

打开chrome浏览器,F12打开console控制台,可以在里面写xml http request代码了。

如图,发送简单请求给不允许跨域的服务器(对于这种简单请求浏览器不会发送预检请求,而是直接发送真正的请求)。可以再修改请求来模拟非简单请求。

default

3 go语言服务器解决跨域问题

可以参考代码 https://github.com/rs/cors

具体流程是判断跨域请求是否是预检请求,如果是预检请求,则处理预检请求,否则处理实际请求。

if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" { c.logf("HandlerFunc: Preflight request") c.handlePreflight(w, r) } else { c.logf("HandlerFunc: Actual request") c.handleActualRequest(w, r) }

@hunterhug
Copy link

弱哦

@wuyongxiu
Copy link
Owner Author

哪里弱?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants