web前端全系列 教程
1839个小节阅读:2244.3k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在开发模式下,如果客户端所在服务器跟后台服务器的地址或者端口不一致,则会出现跨域的问题
服务器运行在3030端口:
xxxxxxxxxx
const express = require('express')
const app = express()
app.get('/api/user', (req, res) => {
res.send({
msg:"来自服务器消息"
})
})
app.listen(3030, () => {
console.log('服务器正在监听3030端口')
})
发送网络请求:
xxxxxxxxxx
const clickProxyHandler = () => {
fetch("http://localhost:3030/api/user")
.then(res => res.json())
.then(data => {
console.log(data)
})
.catch(error => {
console.log(error)
})
}
使用Proxy解决跨域
在package.json中设置Proxy属性:
xxxxxxxxxx
"proxy":"http://localhost:3030"
设置代理后,则发送的api请求会被代理服务器转发到 localhost:3030
提示:
proxy代理仅在开发中有效。
在生产环境下,这个代理无效。