当前位置:网站首页>如何解决跨域
如何解决跨域
2022-06-30 09:52:00 【YF-SOD】
jsonp
利用html中script脚本不跨域解决。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function getData(data) {
console.log(data)
}
</script>
<script src="http://localhost:8080"></script>
</body>
</html>服务端代码
const http = require('http')
const server = http.createServer((req, res) =>
let url = req.url
//设置'Access-Control-Allow-Origin': '*'解决跨域
//res.writeHead(200, {
// 'content-type': 'application/json;charset=utf-8',
// 'Access-Control-Allow-Origin': '*'
//})
res.write('getData("hello")')
res.end()
})
server.listen(8080,()=>{
console.log('localhost:8080')
})此时会打印hello字符串,注意通常的get和post请求服务端的数据返回的是‘getData("hello")'的字符串,只有在script中才会作为js代码执行。
设置请求头Access-Control-Allow-Origin:*
例如上图注释部分。
边栏推荐
- Oracle creates a stored procedure successfully, but the compilation fails
- ArcGIS Pro脚本工具(5)——排序后删除重复项
- 移植完整版RT-Thread到GD32F4XX(详细)
- “昆明城市咖啡地图”活动再度开启
- CVPR 2022 | 清华&字节&京东提出BrT:用于视觉和点云3D目标检测的桥接Transformer
- Notes on numerical calculation - iterative solution of linear equations
- Use keil5 software to simulate and debug gd32f305 from 0
- 滴滴开源敏捷测试用例管理平台!
- mysql数据库基础:存储过程和函数
- Dow Jones Industrial Average
猜你喜欢

Koreano essential creates a professional style

2022 Season 6 perfect children's model toxon division finals came to a successful conclusion

Dyson design award, changing the world with sustainable design

Basic MySQL operation commands of database

Yixian e - commerce publie un rapport trimestriel: adhérer à la R & D et à l’investissement de la marque, réaliser un développement durable et de haute qualité
[email protected]基于51系列单片机的智能仪器教具"/>技能梳理[email protected]基于51系列单片机的智能仪器教具

Dow Jones Industrial Average

Getting started with X86 - take over bare metal control

今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计

机器学习面试准备(一)KNN
随机推荐
乡村振兴公益基金启动暨古茶树非遗保护公益行发布
Questions about cookies and sessions
Dyson design award, changing the world with sustainable design
Use keil5 software to simulate and debug gd32f305 from 0
05_Node js 文件管理模块 fs
Leetcode question brushing (IV) -- greedy thought (go Implementation)
Foster design method
郭琳加冕 2022第三季完美大师 全球人气季军
Leetcode question brushing (I) -- double pointer (go Implementation)
MySQL index, transaction and storage engine of database (1)
“昆明城市咖啡地图”活动再度开启
2022 Season 6 perfect children's model toxon division finals came to a successful conclusion
【Rust日报】2021-01-23 几个新库发布
Deploy lvs-dr cluster
"Hackers and painters" -- why not be stupid
Why can't you rob scientists of NFT
Robotframework learning notes: environment installation and robotframework browser plug-in installation
GD32 RT-Thread DAC驱动函数
著名画家史国良《丰收时节》数字藏品上线长城数艺
Oracle creates a stored procedure successfully, but the compilation fails