I. Jsonp method
1. Principle
In JS, it is not possible for us to request data from different domains directly with XMLHttpRequest. However, it is possible to introduce JS script files from different domains on the page.
2. Format
<script>
function DoSomething (jsondata) {
Todo
};
</script>
<script>http://example.com/data.php?callback=doSomething</script>
Two. use the newly introduced Window.postmessage method in HTML5 to transfer data across domains
1. Get the Window object. For example: var winobj = element. Contentwindow;
2. Format
A.html
<script>
function onload () {
var iframe = document.getElementById (' iframe ');
var winobj = iframe. Contentwindow;
WinObj. PostMessage (' Haha, I am a message from page a ', ' * ');
}
</script>
<iframe>id = "iframe" src = "http://www.test.com/b.html" onload= "onload ()" </iframe>
B.html
<script>
Window.onmessage = function (e) {
E = e | | Event
Alert (E.data); The PostMessage () method is stored in data.
}
</script>
201507152326_: 4 ways to implement JavaScript across domains--Introduction to JSONP and HTML5 methods