33

记录一次跨域访问的傻逼debug

 5 years ago
source link: https://www.tuicool.com/articles/jMBfQbb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

前端使用XMLHttpRequest向后端发请求,由于前后端分离模式,所以分开部署,因为域名也不同,故这个跨域访问搞了我一整天时间不得其解。 其实我早就解决了,网上什么资源都有,只不过是后台返回的response的header加个Access-Control-Allow-Origin: * 。但是傻逼就傻逼在我是请求成功(即业务逻辑上的请求成功)下才设置header,而前端代码里发送的时候得到的response则是业务逻辑错误的回复(因为贪方便,故请求未带上token),故没有这个header,使得我认为后台明明加了这个header,为什么前端还是一直报这个错误。 理论上来说,这么试多次之后,应该早就发现后台代码有问题,但更傻逼的是,我用postman去测试,带token,得到成功的response,header里是有Access-Control-Allow-Origin: * 的,使得我老是以为是前端代码的问题,就这么搞了一天。 真傻吊。

入群交流(该群和以上内容无关):Go中文网 QQ交流群:731990104 或 加微信入微信群:274768166 备注:入群; 公众号:Go语言中文网


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK