CORS 跨域描述
Cross-Origin Resource Sharing (CORS)跨域资源共享。所谓跨域指A页面想获取B页面资源,如果两者的协议、域名、端口或者子域名不同,如A是ip地址,B是域名之间的访问行动都是跨域。浏览器基于安全问题通常会限制跨域访问,不允许跨域请求资源。
问题描述
当跨域调用时Chrome浏览器出现Uncaught ReferenceError: ok is not defined,A域www.abc.com B域 192.168.1.100。B应用一切正常,返回信息dial: ok,819,8008613542189232,A应用无法获得B域的返回信息,从而无法解析。
当B没有返回任何信息时,也会出现如上错误,这属于正常。比如当B时一个CallCenter,A调用B拨打电话,但是A拨通后,没有人工拿起电话,B同样没有任何返回。
同样的场景在edge瀏覽器下也出现如上错误,在獵豹瀏覽器下正常。
Chrome版本 83.0.4103.116 下没有如上错误,预计在版本91之前都不存在如上错误。
解决方法
方法1 客户端,通过注册表禁止preflight策略解决
此方法仅限于Chrome版本在91-101之间,101之后将强制preflight策略。请参考Chrome在版本91后,preflight验证请求造成两次请求的问题。
方法2 服务端,通过Jsonp请求,以及Server header设置
## 这一点很重要,可以把私有网络做一个域名,这样更方便使用。
Again, say bar.example resolves to 192.168.1.1.
Modern browsers will not block cross-domain requests outright. If Site A requests a page from Site B, the browser will actually fetch the requested page on the network level and check if the response headers list Site A as a permitted requester domain. If Site B has not indicated that Site A is allowed to access this page, the browser will trigger the XMLHttpRequest’s error event and deny the response data to the requesting JavaScript code.1
被访问的website B,必须返回Access-Control-Allow-Origin: http://siteA.com,siteA是发起请求的website。
方法三设置SameSite by default cookies、Cookies without SameSite must be secure
–disable-features=SameSiteByDefaultCookies,CookiesWithoutSameSiteMustBeSecure
在chrome的快捷方式右击–>属性
在目标输入框最后面加一个空格,然后粘贴以下内容,应用保存就可以了。
方法四Insecure origins treated as secure
chrome://flags/
方法五通过Nginx代理服务器的方式解决
需要搭建一台Nginx服务,其它同仁有成熟的解决方案,这边不再说明。
一个具有注脚的文本。1