JSONP的概念
-
JSONP指带有padding的JSON
格式:getTheAnimal({ "animal": "cat" });
解析:内联于JSON文档的javaScript调用了一个函数,函数参数是JSON。 -
作用:跨域传输(script标签不受同源策略的影响)
-
使用:
函数在客户端定义,数据在服务端获取,使用script标签进行数据请求
客户端代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function showInfo(data) {
console.log(data)
}
</script>
<script src=" http://127.0.0.1:3000/getscript?callback=showInfo"></script>
</body>
</html>
服务端代码:
// 导入 http 内置模块
const http = require('http')
// 这个核心模块,能够帮我们解析 URL地址,从而拿到 pathname query
const urlModule =