出现这个玩意就是跨域请求了
解决办法很多,这里配置

      '/apis': {
        // 测试环境
        target: 'http://www.thenewstep.cn/',  // 接口域名
        changeOrigin: true,  //是否跨域
        pathRewrite: {
            '^/apis': ''   //需要rewrite重写的,
        }  



注意配置好之后项目要重启才能生效
fetch请求
 

created() {
    fetch('/apis/test/xxxx.php', {
        //请求方法
        method: 'post',
        headers: {
            //返回给客户端的类型
            'Content-type': 'application/json',
            //携带tooken
            token: "fdggsdflskgg89ssfgss048728dfd59dh"
        },
        //发送的数据
        body: JSON.stringify({username: 'admin', password: '123456'})

    })
        .then(result => {
            return result.json()
        })
        .then(data => {
            console.log(data)
        })
}

axios
下载 npm install axios -D

this.$axios.post("/apis/test/xxxx.php",{
    username:'hello',
    password:'2222'
})
    .then(data=>{
        //解构赋值
        //判断状态码替换数据
    })

Logo

前往低代码交流专区

更多推荐