$.ajax()方法概述

作用: 发送jsonp请求

$.ajax({
  url: 'http://www.example'
//指定单签发送jsonp请求
  dataType: 'jsonp',

  success: function(response) {}
})

jquery将jsonp代码一并封装了进去,ajax是同过ajax对象请求,jsonp是通过创建script标签发送请求。发送不同的请求,在$.ajax内部的方法不一样。如何才能确定发送的是什么请求,这个时候需要使用调用dataType这个参数,jsonp 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="btn">发送请求</button>
    <script src="js/jquery.min.js"></script>
    <script>
        function fn(response){
            console.log(response)
        }
        $('#btn').on('click', function(){
            $.ajax({
                url: '/jsonp',
                //向服务器端传送函数名称的参数名称
                jsonp: 'cb',
                jsonpCallback: 'fn',
                //代表现在要发送的是jsonp请求
                dataType: 'jsonp',
                // success: function(response){
                //     console.log(response)
                // }

            })
        })
    </script>
</body>
</html>

 

 

更多推荐

$.ajax() 发送jsonp请求