"Principle: It leverages the 'src' attribute within the <script> tag to request a server-side address. However, the response from this address is not in a directly usable object or array format; instead, it's a call to a global function. The data that was originally intended to be returned to the client is passed to this function as parameters."
var script = document.createElement("script");
script.src = "http://~~~~~~?callback=foo";
var headLabel = document.getElementsByTagName("head")[0];
headLabel.appendChild(script);
function foo(data){
console.log(data); // data is the content that we want
}
原理:借助<script>标签中的src属性请求服务端的一个地址,但这个地址返回的不是能够可以直接用的对象格式的数据或者数组形式的数据,而是一个全局函数的调用,原本需要返回给客户端的数据通过参数传递给这个函数。
var script = document.createElement("script");
script.src = "http://~~~~~~?callback=foo";
var headLabel = document.getElementsByTagName("head")[0];
headLabel.appendChild(script);
function foo(data){
console.log(data); // data is the content that we want
}