当前位置:网站首页>1-21 JSONP接口
1-21 JSONP接口
2022-06-30 21:27:00 【画不完的饼】
回顾JSONP的概念与特点
概念:浏览器通过
特点:
- JSONP不属于真正的Ajax请求,因为它没有使用XMLHttpRequest这个对象。
- JSONP仅支持GET请求,不支持POST、PUT、DELETE等请求。
创建JSONP接口的注意事项
如果项目中已经配置了CORS跨域资源共享,为了防止冲突,必须在配置CORS中间件之前声明JSONP的接口。否则JSONP接口会被处理成开启了CORS的接口。
实现JSONP接口的步骤
- 获取客户端发送过来的回调函数的名字
- 得到要通过JSONP形式发送给客户端的数据
- 根据前两部得到的数据,拼接出一个函数调用的字符串。
- 把上一步拼接得到的字符串,响应给客户端的
//必须在配置cors之前配置JSONP
app.get('/api/jsonp',(req,res)=>{
//得到函数的名称
const funcname = req.query.callback
const data = {
name:'zs',
age:22
}
const scriptStr = `${funcname}(${JSON.stringify(data)})`
res.send(scriptStr)
})
边栏推荐
猜你喜欢

《ClickHouse原理解析与应用实践》读书笔记(3)

Arcmap|assign values to different categories of IDS with the field calculator

Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
测试媒资缓存问题

Go build server Foundation

ArcGIS构建发布简单路网Network数据服务及Rest调用测试

ArcGIS construction and release of simple road network data service and rest call test

Et la dégradation du modèle de génération de texte? Simctg vous donne la réponse

文本识别-SVTR论文解读
笔记【JUC包以及Future介绍】
随机推荐
. NETCORE redis geo type
文本识别-SVTR论文解读
Fletter nested hell? No, constraintlayout to save!
本地浏览器打开远程服务器上的Jupyter Notebook/Lab以及常见问题&设置
ICLR'22 Spotlight | 怎样度量神经网络权重中的信息量?
What happens when word encounters an error while trying to open a file?
防范未授权访问攻击的十项安全措施
Side sleep ha ha ha
Coefficient of variation method matlab code [easy to understand]
MySQL advanced 3
“信任机器”为发展赋能
ceshi deces
Metauniverse may become a new direction of Internet development
Test medal 1234
CA I ah, several times Oh, ah, a sentence IU home Oh
vim 常用快捷键
Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
Et la dégradation du modèle de génération de texte? Simctg vous donne la réponse
物联网僵尸网络Gafgyt家族与物联网设备后门漏洞利用
时空数据挖掘:综述