GET http://localhost:3000/api/auth/user/:id 401 (nicht autorisiert)
P粉232409069
P粉232409069 2024-03-19 16:49:19
0
1
421

Ich verwende vue3 mit Axios und Prisma, habe aber Probleme beim Abrufen von Benutzerinformationen.

Meine Postman-Anfrage funktioniert (http://localhost:3000/api/auth/user/7), meine Axios-Anfrage jedoch nicht.

Kannst du mir helfen?

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

Antworte allen(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);
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!