Heim  >  Fragen und Antworten  >  Hauptteil

GET http://localhost:3000/api/auth/user/:id 401 (nicht autorisiert)

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粉232409069P粉232409069214 Tage vor419

Antworte allen(1)Ich werde antworten

  • P粉322106755

    P粉3221067552024-03-20 15:37:00

    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);
    

    Antwort
    0
  • StornierenAntwort