一、什么是跨域

jsonp跨域实例

二、如何解决跨域

1.前端常用 JSONP

2.服务器端配置 HTTP 协议的 header 解析

三、JSONP实现的实例

<!DOCTYPE html>
<html>
<head>
    <title>jsonp跨域test</title>
</head>
<body>
    <script type="text/javascript">
        function res (r) {  //定义回调函数接收返回结果
            console.log(r)
        }
    </script>
    <script src="./api.json?callback=res">
//? 之前为请求的路径
     //? 之后 callback 可缩写为 cb,值为回调函数的名称
</
script> </body> </html>

请求的 json文件内容如下:

res({"data":"js"})

去浏览器打开 html 文件,打开控制台,效果如下:

jsonp跨域实例

 

 

 

 

 

 

jsonp跨域实例

 

相关文章:

  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
  • 2018-05-01
  • 2018-07-03
  • 2018-08-26
猜你喜欢
  • 2022-12-23
  • 2021-07-29
  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-10-05
  • 2021-08-07
相关资源
相似解决方案