uni-app H5跨域请求解决方案

uni-app本机应用程序:http://localhost:8080

接口api地址:http://localhost:35949/Test

 

打开manifest.json配置文件,最底部点击"源码视图",在"h5"中加入配置,

prot为前端应的端口,taget为后端服务的地址+端口

"h5":{
        "devServer":{
            "port":8080, //应用程序端口
            "disableHostCheck":true,
            "proxy":{
                "/api":{
                    "target":"http://localhost:35949/", //后端服务端口
                    "changeOrigin":true,
                    "secure":false,
                    "pathRewrite":{"^/api":"/"}
                }
            }
        }
    }

 

proxy内的配置项表示会将 target内的地址替换为 pathRewrite的值,

配置后请求的方法:

				uni.request({
					url: 'api/Test',
					method: 'POST',
					data: {},
					success: res => {},
					fail: () => {},
					complete: () => {}
				});

  请求的地址会 自动替换为:http://localhost:8080/api/Test

posted @ 2021-06-16 21:26  陈鹏昱Chen  阅读(2397)  评论(0编辑  收藏  举报