console
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
function getJSONP(url_, callback) {
var url=url_;
if (!url) { return; }
var a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
r1 = Math.floor(Math.random() * 10);
r2 = Math.floor(Math.random() * 10);
r3 = Math.floor(Math.random() * 10);
var name = 'getJSONP' + a[r1] + a[r2] + a[r3];
var cbname = 'getJSONP.' + name;
if (url.indexOf('?') == -1) {
url += "?jsonp=" + cbname;
}else {
url += "&jsonp" + cbname;
}
getJSONP[name] = function (data) {
try {
callback && callback(data);
}
catch (e) {}
finally {
delete getJSONP[name];
script.parentNode.removeChild(script);
}
}
var script = document.createElement('script');
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
console.log("sdfadfaf");
getJSONP("https://class.imooc.com/api/jsonp", function (data) {
console.log(data);
});
$("#btn").click(function(){
console.log("dfasdfa");
$.ajax({
async : true,
url : "https://api.douban.com/v2/book/search",
type : "GET",
dataType : "jsonp",
jsonp : 'callback',
jsonpCallback: 'handleResponse',
data : {
q : "javascript",
count : 1
},
success: function(response, status, xhr){
console.log('状态为:' + status + ',状态是:' + xhr.statusText);
console.log(response);
}
});
});
});
</script>
</head>
<body>
<div id="mydiv">
<button id="btn">点击</button>
</div>
</body>
</html>