跨域问题之JSONP

xiaoxiao2021-02-28  38

<script> //假设服务器地址是http://domain.com/getData 并且运行jsonp方式访问 var script = document.createElement('script'); script.setAttribute('src', "http://domain.com/getData?cb=fn1"); var header =document.getElementsByTagName('head')[0]; header.appendChild(script); function fn1(data) { console.log(data); } </script>

1、script标签

2、在script标签加载资源是没有跨域问题的

    在资源加载进来之前定义好一个函数,这个函数接收一个参数(数据),函数里面利用这个参数做某些事情,然后需要的时候通过script标签加载对应远程文件资源,当远程的文件资源被加载进来的时候,就会去执行我们前面定义好的函数,并且把数据当作这个函数的参数传入进去

转载请注明原文地址: https://www.6miu.com/read-2630173.html

最新回复(0)