How does uniapp conduct local cross-domain testing when writing H5?
First, when testing locally,
The access link address is http://localhost:8080/#/ , but the API requesting the server has a domain name. If cookie verification is performed, or in other circumstances, cross-domain problems will occur when making requests. .
You can refer to the following code in the manifest.json file:
"h5" : {
"publicPath" : "./",
"router" : {
"mode" : "history",
"base" : "./"
},
"title" : "testDemo",
"optimization" : {
"treeShaking" : {
"enable" : true
}
},
"devServer" : {
"port" : 8080, //浏览器运行端口
"https" : false,
"disableHostCheck" : true,
"proxy" : {
"/dpc" : {
//修改配置跨域代理路由(需要重启、需要重启、需要重启)
"target" : "https://xxxxxx.com",//你自己的API对应的域名
"changeOrigin" : true,
"secure" : false,
"pathRewrite" : {
"^/dpc" : ""
}
}
}
}
}
After the cross-domain modification is completed, the method of restarting is just one click.
The request in the request interface needs to be replaced with a domain name. Assume that the link address of your request API is
https://xxxxx.com/api/getgoodsList
When requesting uni.request({
url:'/dpc'+'/api/getgoodsList',//It is equivalent to replacing your original domain name for requesting server data with pathRewrite in the configuration agent
method:'get'||'post',
.....
})
Okay, here it is, OK. . . .
Front-end uniapp request cross-domain request is so simple, welcome to exchange and learn.