<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
1.jsonp a.js为服务器数据,是一个函数调用,具体的函数在客户端
<script>
function show (data) {
console.log(data.name, data.age);
}
</script>
<script>
// 服务端函数调用
// a.js
show({
name: 'kang',
age: 23
});
</script>
<hr>
2.百度搜索是一个jsonp,引入后,本地实现show2方法
<script>
function show2 (data) {
console.log(data.s);
}
</script>
<script src="https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=qq&cb=show2" charset="utf-8"></script>
<hr>
3.引入后端的jsonp,本地实现方法
<script>
function show ({s}) {
console.log(s);
}
window.onload = function () {
let oTxt = document.getElementById('txt1');
oTxt.oninput = function () {
let url = `https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=${this.value}&cb=show`;
let oS = document.createElement('script');
oS.src = url;
oS.onload = function () {
oS.parentNode.removeChild(oS); // 追加script,执行完又删掉
};
document.head.appendChild(oS);
};
};
</script>
<input type="text" id="txt1">
</body>
</html>