当前位置:网站首页>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)
})
边栏推荐
猜你喜欢

Go build server Foundation

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

毕业五年,想当初若没有入行测试,我是否还会如这般焦虑

Reading notes of Clickhouse principle analysis and Application Practice (1)

asp. Net core JWT delivery

asp.net core JWT传递

双立体柱状图/双y轴

Phoenix architecture: an architect's perspective

Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test

go搭建服务器基础
随机推荐
防范未授权访问攻击的十项安全措施
Who are you and I
根据肠道微生物组重新思考健康饮食
sdfsdf
Sqlserver string type converted to decimal or integer type
遇到“word在试图打开文件时遇到错误”怎么办?
jenkins下载插件下载不了,解决办法
【无标题】
用yml文件进行conda迁移环境时的报错小结
ca i啊几次哦啊句iu家哦
Open source internship experience sharing: openeuler software package reinforcement test
数字货币:影响深远的创新
How to run jenkins build, in multiple servers with ssh-key
1-20 预检请求
pytorch geometric torch-scatter和torch-sparse安装报错问题解决
FreeRTOS record (IX. an example of a bare metal project transferring to FreeRTOS)
雷达数据处理技术
Flutter 嵌套地狱?不存在的,ConstraintLayout 来解救!
布隆过滤器
Oprator-1 first acquaintance with oprator