我得到了這個測驗代碼:
import axios from 'axios';
const readWithAxios = async (basicAuth, user, passwd) => {
let options = {
auth: {
username: user,
password: passwd
},
headers: { "Content-Type": "application/json"},
withCredentials: true
};
return axios.get('https:///geolite.info/geoip/v2.1/country/me?pretty', options);
}
const readWithFetch = (basicAuth) => {
return new Promise(res=>{
let headers = new Headers();
headers.set('Authorization', basicAuth);
fetch('https://geolite.info/geoip/v2.1/country/me?pretty', {
method: 'GET',
headers: headers,
}).then(response => res(response.json()));
})
}
const readData = async () => {
let user = '<my_api_user>';
let passwd = '<my_api_key>';
let basicAuth = 'Basic ' Buffer.from(user ":" passwd).toString('base64');
let geoData;
//geoData = await readWithFetch(basicAuth);
geoData = await readWithAxios(basicAuth, user, passwd);
console.log(geoData);
}
readData();
我試圖理解為什么 readWithFetch 作業正常而 axios 連接被拒絕。這是一個簡單的基本身份驗證……沒什么特別的。
我已經嘗試了所有這些 readWithAxios 版本:
版本 1
const readWithAxios = async (basicAuth, user, passwd) => {
let options = {
auth: {
username: user,
password: passwd
},
headers: { "Content-Type": "application/json"},
withCredentials: true
};
console.log('options', options);
return axios.get('https:///geolite.info/geoip/v2.1/country/me?pretty', options);
}
版本 2
const readWithAxios = async (basicAuth, user, passwd) => {
let options = {
auth: {
username: user,
password: passwd
},
headers: { "Content-Type": "application/json", 'Authorization': basicAuth},
withCredentials: true
};
console.log('options', options);
return axios.get('https:///geolite.info/geoip/v2.1/country/me?pretty', options);
}
版本 3
const readWithAxios = async (basicAuth, user, passwd) => {
let options = {
method: 'GET',
url: 'https:///geolite.info/geoip/v2.1/country/me?pretty',
auth: {
username: user,
password: passwd
},
headers: { "Content-Type": "application/json", 'Authorization': basicAuth},
withCredentials: true
};
版本 4
return axios(options);
}
const readWithAxios = async (basicAuth, user, passwd) => {
let options = {
method: 'GET',
url: 'https:///geolite.info/geoip/v2.1/country/me?pretty',
headers: { "Content-Type": "application/json", 'Authorization': basicAuth},
withCredentials: true
};
return axios(options);
}
readWithAxios 的正確寫法是什么?
uj5u.com熱心網友回復:
https:///geolite您在 Axios 版本中有一個三重斜線。它應該是https://
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/534369.html
