Ajax - 跨域 dataType: ‘jsonp‘

1.跨域体验

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="./libs/jquery/jquery.js"></script>
  </head>
  <body>
    <button>发起跨域请求</button>
    <script>
      // document.querySelector('button').onclick = function() {
      //   let xhr = new XMLHttpRequest()
      //   xhr.open('get', 'http://127.0.0.1:3001/getStudentsJSONP.js')
      //   xhr.send()
      // }
      $('button').on('click', function() {
        $.ajax({
          url: 'http://127.0.0.1:3001/getStudentsJSONP.js',
          // 跨域请求
          dataType: 'jsonp',
          success: function(res) {
            console.log(res)
          }
        })
      })
    </script>
  </body>
</html>

2.jsonp跨域的原理

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="./libs/jquery/jquery.js"></script>
    <link
      rel="stylesheet"
      href="https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superui/css/ubase_9376fdcf.css"
    />
  </head>
  <body>
    <button>发起跨域请求</button>
    <script>
      function test(res) {
        console.log(res)
      }
    </script>
    <!-- <img
      src="https://img2.baidu.com/it/u=1931533531,716385319&fm=26&fmt=auto&gp=0.jpg"
      alt=""
    /> -->
    <script src="http://127.0.0.1:3001/getStudentsJSONP.js?callback=test">
      // 跨域的原理:
      // 1.src和href天然可以跨域,没有任何的跨域请求的限制
      // 2.jsonp跨域的本质是利用了script标签的src属性的天然跨域特性
      // 3.具体的操作是:在发起跨域请求的时候传入一个本地拥有的函数名称,服务器会返回调用函数的形式,且拼接好返回的数据
      // 4.通过script请求获取的内容会默认以js语法来解析

      // 5.jsonp跨域和异步对象没有任何的关系
      // 6.jsonp只能发起Get请求
    </script>
  </body>
</html>

3.jsonp的原理

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function abc(res) {
        console.log(res)
      }
    </script>
    <!-- 通过script的src属性发出的请求获取的返回内容默认会为js语法解析 -->
    <!-- <script src="http://127.0.0.1:3002/getdata?callback=success"></script> -->

    <script>
      let $ = {
        ajax: function(option) {
          if (option.dataType === 'jsonp') {
            // 1.创建一个script标签
            let script = document.createElement('script')
            // 2.为script标签设置src属性
            script.src = option.url + '?callback=abc'
            // 3.将script添加到body中
            document.body.appendChild(script)
          } else {
            let xhr = new XMLHttpRequest()
            // /.....
          }
        }
      }

      $.ajax({
        url: 'http://127.0.0.1:3002/getdata',
        dataType: 'jsonp'
      })
    </script>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Henry_ww

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值