uniapp h5本地域名调试解决跨域
前提:
1.本地创建的测试站点,例如 xxx.test
2. 本地使用nginx
因项目配置不同,devServer转发,可以直接在csdn找uniapp 调试解决跨域的文章来配置
问题:
遇到post提交跨域
解决
1 使用hbuilderx内置浏览器
唯一缺点,查看接口请求麻烦
2 安装浏览器cors插件+本地nginx设置跨域
location / {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';# 针对OPTIONS预检请求,直接返回204状态码,避免被转发到后端if ($request_method = 'OPTIONS') {add_header 'Access-Control-Max-Age' 1728000; # 预检请求缓存20天add_header 'Content-Type' 'text/plain charset=UTF-8';add_header 'Content-Length' 0;return 204;}
}