Skip to content

Commit

Permalink
前端预先定义回调函数, 后端返回一个函数调用
Browse files Browse the repository at this point in the history
  • Loading branch information
luoquanquan committed Dec 5, 2018
1 parent 5bdc0dd commit a2e0ba9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion be/jsonp/index.js
Expand Up @@ -4,7 +4,7 @@ const PORT = 8888;

// 创建一个 http 服务
const server = http.createServer((request, response) => {
response.end("var aaaa = {name: 'quanquan', friend: 'guiling'}");
response.end("aaaa({name: 'quanquan', friend: 'guiling'})");
});

// 启动服务, 监听端口
Expand Down
8 changes: 4 additions & 4 deletions fe/jsonp/index.html
Expand Up @@ -10,10 +10,10 @@
<h3>jsonp 实现跨域</h3>

<script>
// 第一次因为还没有引入外部 js 所以打印 undefined
console.log(window.aaaa)
// 1 秒后, 外部 js 加载完成, 能打印出后端返回的变量定义
setTimeout(() => {console.log(window.aaaa)}, 1e3)
// 由于后端返回的内容即将调用函数 aaaa, 那我们就预先定义一个呗, 这东西就叫回调函数
function aaaa(param) {
console.log('后端返回的参数是: ', param)
}
</script>
<script src="http://localhost:8888/"></script>
</body>
Expand Down

0 comments on commit a2e0ba9

Please sign in to comment.