एक्सयूआई अनुरोध HTTP के साथ Vue लॉगिन
Nov 26 2020
Vue में Im नया और मेरे बैकएंड से एक अनुरोध HTTP बनाने की कोशिश कर रहा हूँ,
जब मैं अपने ब्राउज़र में निरीक्षण करता हूं, तो मुझे प्रवेश / लॉगिन से टोकन प्राप्त होता है लेकिन एपीआई / उपयोगकर्ताओं में मुझे "टोकन इनविटेशन" मिलता है। मैं अपने एपीआई / उपयोगकर्ताओं का डेटा कैसे प्राप्त करूं?
import axios from "axios";
export default {
name: "login",
async created() {
const response = await axios.get("api/users", {
headers: {
Authorization: "Bearer " + localStorage.getItem("token")
}
});
console.log(response);
},
data() {
return {
showError: false,
email: "",
password: "",
};
},
methods: {
async EnvioLogin() {
try {
const response = await axios.post("api/auth/login", {
email: this.email,
password: this.password,
});
localStorage.setItem("token", response.data.token);
const status = JSON.parse(response.status);
if (status == "200") {
console.log(response);
this.$router.push("intermediorotas");
}
} catch (error) {
this.showError = true;
setTimeout(() => {
this.showError = false;
}, 3000);
}
},
},
जवाब
2 x-rw Nov 26 2020 at 22:55
आप बैकएंड पर कॉल करने के लिए एक सेवा बना सकते हैं, मुझे लगता है कि समस्या url है http: // local स्क्रीनशॉट: 3000 / api, आपने इस भाग को याद किया http: // local स्क्रीनशॉट: 3000
import axios from 'axios'
const client = axios.create({
baseURL: 'http://localhots:3000/api',
headers: {
'Content-Type': 'application/json',
},
})
export default client
फिर सेवा आयात करें
import myService from './myService'
await myService.get(`/auth/login`, {})