jq ajax 利用 dataType:"jsonp" 解决跨域

2020-05-02  本文已影响0人  lessonSam

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.11.0/jquery.js"></script>
    <title>Document</title>
  </head>
  <body>
    <script>
      // $.get(),$.post $.().load()
      $(function () {
        $.ajax({
          type: 'get',
          url: 'https://api.asilu.com/weather/',
          data: {
            city: '深圳',
          },
          dataType: 'jsonp', // 会自动的创建script 标签 解决跨域

          success: function (res) {
            console.log(res);
          },
          error: function (msg) {
            console.log(msg);
          },
        });
      });
    </script>
  </body>
</html>

上一篇下一篇

猜你喜欢

热点阅读