router.get('/api/zhangsan',function (req,res,next){
res.json({
code:0,
a:1
})
})```
```javascript
<script>
import axios from 'axios'
export default {
created(){
axios({
url:'http://http://localhost:3000/api/zhangsan'
}).then(res=>{
console.log(res);
})
}}
</script>
odule.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000/',
ws: true,
changeOrigin: true
},
'/foo': {
target: '<other_url>'
}
}
}
}```
```javascript
router.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
//Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', '*');
res.header('Content-Type', 'application/json;charset=utf-8');
next();
});
两种方法解决跨域问题