axios跨域配置好了,但是点击一点反应都没有,有时候跨域失败,(但是配置没错啊)求大佬帮忙
import axios from 'axios'
Vue.prototype.$axios = axios
axios.defaults.baseURL = '/api'
module.exports = {
devServer: {
open: true,
host: '127.0.0.1',
port: 8080,
https: false,
hotOnly: false,
proxy: { // 配置跨域
'/api': {
target: 'http://localhost:3000',
ws: true,
changOrigin: true,
pathRewrite: {
'^/api': ''
}
}
},
before: app => { }
}
}
<template>
<div class="test">
名字:{user.name}
年龄: {user.age}
<input type="submit" @click='submit'>
</div>
</template>
<script>
export default {
name : 'test',
data() {
return {
user : []
}
},
methods : {
submit(){
this.$axios.get('/api/test').then(result =>{
console.log(result)
this.name = result.name
this.age = result.age
}).catch(err =>{
console.log(err)
})
}
}
}
</script>
router.get('/test',(req,res)=>{
let user ={
name : 'tw',
age : 18
}
return res.json(user)
})
点击submit,控制台没一点反应,什么东西都不显示
你试试把sbumit方法得第一行加上console.log("c成功")应该也不会输出,你是用vue写得代码么?vue得methods不应该是 submit:function(){}这种么?
'/api': {
target: 'http://api.zhuishushenqi.com',//设置你调用的接口域名和端口号
changeOrigin: true, //跨域
pathRewrite: {
'^/api': '/' //这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://10.1.5.11:8080/xxx/duty?time=2017-07-07 14:57:22',直接写‘/api/xxx/duty?time=2017-07-07 14:57:22’即可
}
},
推荐你一个软件 postman 前端必备 可以查看接口是否成功 以后出现跨域问题别慌 , 在确定后台接口没问题的情况下 可以先查看自己是否有书写问题 每没有的话 看看参数是都对应 如果在找不到 抓包 看自己的请求是详情数据