什么叫跨域
跨域解決方法:
1.cors(簡單請求、非簡單請求)
2.jsonp
3.new Image()
4.nginx反向代理
5.iframe(postmessage、document.domain、window.name、location.hash)
- location.hash(此方法需要三個頁面,index.html主頁面、data.html異域頁面、proxy.html代理頁面。其中proxy跟index要同源
index嵌套iframe,指向data.html.data.html嵌套iframe,指向proxy.html。這樣的話,data.html加載數據后把數據放到proxy的hash中。proxy中通過監聽hash的變化,然后通過window.parent.parent獲取主頁面的window實例) - window.name(此方法利用window.name在iframe的src的變化時不會改變的原理。需要兩個頁面,index.html和data.html。data.html在獲取到數據后將數據存到window.name中。然后跳轉到跟index.html同源的頁面。然后index.html就可以通過iframe的window.name獲取數據)
6.fetch跨域