JSONP跨域完成获取信息

Time: 2024-07-21 Sunday 14:49:01
Author: Jackasher

JSONP跨域完成获取信息

服务器端返回函数信息

1
2
3
4
app.get("/jsonp",(req,res) => {
res.type("application/javascript")
res.send("addTestOfDiv()")
})

动态创建script获取数据返回,执行回调函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<html>

<head>
<script src="typejavascript.js"></script>
</head>

<body>
<input type="text">
<button id="button">发送jsonp请求</button>
<div id="div"></div>
<script>
function addTestOfDiv(){
const div = document.getElementById("div")
div.innerText = "Hi Jsonp"
}

const button = document.getElementById("button")
button.onclick = function(){
const script = document.createElement('script')
console.log("点击了!!!");
script.src = "http://127.0.0.1:3000/jsonp"

document.body.append(script)
}


</script>
</body>

</html>

JSONP跨域完成获取信息
http://example.com/2024/07/21/JSONP跨域完成获取信息/
作者
Jack Asher
发布于
2024年7月21日
许可协议