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


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

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

 

打开manifest.json配置文件,在"h5"中加入配置:

"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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM