注:前端配置跨域后服务器端(Nginx)也需要配置,否则接口无法访问
vue跨域
配置文件 /vue.config.js
devServer: {
port: 7100,
proxy: {
'/api': {
target: 'http://域名',
changeOrigin: true,
logLevel: 'debug',
pathRewrite: { '^/api': '' }
}
}
}
调用
import axios from 'axios'
const headers = {
'Content-Type': 'application/json;charset=utf-8'
}
axios.post('/bryant/接口地址', headers).then((res) => {
cosole.log(res)
})
uniapp跨域
配置文件 /manifest.json
"devServer" : {
"https" : false,
"port" : 7101,
"proxy": {
"/api": {
"target": "https://域名",
"changeOrigin": true,
"secure": false,
"pathRewrite":{"^/api":""}
}
}
},
调用
uni.request({
url: '/api/接口',
method: 'POST',
success:ret=>{
console.log(ret)
}
})