GET http://localhost:3000/api/auth/user/:id 401(未经授权)
P粉232409069
P粉232409069 2024-03-19 16:49:19
[Vue.js讨论组]

我将 vue3 与 axios 和 prisma 结合使用,但在获取用户信息时遇到问题。

我的邮递员请求可以(http://localhost:3000/api/auth/user/7),但我的 axios 请求不行。

你能帮我吗?

async created () {
        const response = await axios.get('http://localhost:3000/api/auth/user/:id', { 
            headers: {
                Authorization: 'Bearer ' + localStorage.getItem('token')
            }
            
        });
        console.log('ici');

        
    }

P粉232409069
P粉232409069

全部回复(1)
P粉322106755

axios不支持URL参数。

一种解决方案是使用模板字符串来构建请求 URL。

例如:

function getID(id) {
    const response = await axios.get(`http://localhost:3000/api/auth/user/${id}`,{ 
          headers: {
              Authorization: 'Bearer ' + localStorage.getItem('token')
          }  
    });
}

// getID(7);
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号