一、跨域解决方法有:
1、设置document.domain---解决无法读取同源网页的cookie问题。
因为浏览器是通过document.domain属性来检查两个页面是否同源,因此只要通过设置相同的document.domain,两个页面就可以共享Cookie(此方案仅限主域相同,子域不同的跨域应用场景。
//两个页面都设置
document.domain='test.com'
2、通过JSONP跨域
JSONP 是服务器与客户端跨源通信的常用方法。但只支持get请求,不支持post。
3、跨文档通信 API:window.postMessage()
调用postMessage方法实现父窗口http://test1.com向子窗口http://test2.com发消息(子窗口同样可以通过该方法发送消息给父窗口)
它可用于解决以下方面的问题:
页面和其打开的新窗口的数据传递
多窗口之间消息传递
页面与嵌套的iframe消息传递
上面三个场景的跨域数据传递
// 父窗口打开一个子窗口
var openWindow = window.open('http://test2.com', 'title');
// 父窗口向子窗口发消息(第一个参数代表发送的内容,第二个参数代表接收消息窗口的url)
openWindow.postMessage('Nice to meet you!', 'http://test2.com');
4、CORS
【前端设置】根据xhr.withCredentials字段判断是否带有cookie
①原生ajax
var xhr = new XMLHttpRequest(); // IE8/9需用window.XDomainRequest兼容
// 前端设置是否带cookie
xhr.withCredentials = true;
xhr.open('post', 'http://www.domain2.com:8080/login', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('user=admin');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
};
②vue-resource
Vue.http.options.credentials = true
③axios
axios.defaults.withCredentials = true
更多请点击https://blog.csdn.net/qq_38128179/article/details/84956552查看
二、前端代码性能优化
1、减少http请求,资源压缩合并,合并图片(如精灵图)
2、非核心代码使用异步加载
异步加载方法:①动态脚本加载.②defer ③async
<script src="./defer1.js" defer></script>
<script src="./async1.js" async></script>
3、利用浏览器缓存等方式。
欢迎补充