回帖:上面方法没啥用,还是得靠Nginx来解决跨域。
1.修改nginx.conf配置文件
找到代码:
listen 80;
server_namelocalhost;
#charset koi8-r;
#access_loglogs/host.access.logmain;
location / {
root html;
indexindex.html index.htm;
}
# 在这里添加代码
location /api{
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
if ($request_method = 'OPTIONS') {
return 200;
}
# 这里是服务器地址
proxy_pass http://localhost:8080;
}
2.启动Nginx
3.使用
uni.request({
url:'http://localhost/api/接口地址',
method: 'POST',
success: (res) => {
console.log(res);
}
});
2024-07-03 11:41

