我正在使用带有NODEJS的 API ,并使用 axios进行“获取” ...
在 NODEJS 中,我们将“ params” 称为之前 “?” 字符,然后我们呼叫“查询” ,将所有在之后 “?” 之后的字符,,例如:
https://www.url.com/testing.xsjs?QueryName1='test1,test1'&QueryName2=1
问题,我在Axios中遇到的问题是无法正确创建网址,正确的网址应为:
https: //www.url.comho/fatSales.xsjs?Shop='shop1,shop2,shop3'&PeriodoDe=201801&PeriodoAte=201807&Kpi='kp1,kp2,kp3'& Select = NUCOMPETEC
但是他为我创建的网址是:
https://www.apiUrl.com/Shop=shop1, + shop2, + shop3&PeriodoDe=201801&PeriodoAte=201807&Kpi=kp1,+kp2,+kp3&Select=NUCOMPETEC
此URL创建的URL有一些问题,如下所示:
1)商店和Kpi它会创建“ +”字符 2)不会在“?”之前添加参数(NODEJS)。角色...
记住: Shop和Kpi(这是一个包含1个或*个元素的数组)
const axios = require('axios');
const Qs = require('qs');
class ApiDAO {
constructor(xsjs, shoppingId, periodOf, periodUntil, kpi, select){
this.xsjs = xsjs;
this.shoppingId = shoppingId;
this.periodOf = periodOf;
this.periodUntil = periodUntil;
this.kpi = kpi;
this.select = select;
}
configAxios(){
return axios.create({
method: 'GET',
responseType: 'json',
responseEncoding: 'utf8',
headers: {
'Content-Type': "application/json",
'Cache-Control': "no-cache",
Authorization: "",
Apikey: "",
},
params: {
xsjs: this.xsjs,
Shop: this.shoppingId,
PeriodoDe: this.periodOf,
PeriodoAte: this.periodUntil,
Kpi: this.kpi,
Select: this.select
},
});
}
async getResponseAxios(){
return await this.configAxios().get('https://www.apiUrl.com/');
}
}
module.exports = () => { return ApiDAO };
答案 0 :(得分:0)
您要对参数进行分类吗?您可以尝试以下方法吗?
const axios = require('axios');
const Qs = require('qs');
class ApiDAO {
constructor(xsjs, shoppingId, periodOf, periodUntil, kpi, select){
this.xsjs = xsjs;
this.shoppingId = shoppingId;
this.periodOf = periodOf;
this.periodUntil = periodUntil;
this.kpi = kpi;
this.select = select;
}
configAxios(url){
return axios.get({
method: 'GET',
url: url,
responseType: 'json',
responseEncoding: 'utf8',
headers: {
'Content-Type': "application/json",
'Cache-Control': "no-cache",
Authorization: "",
Apikey: "",
},
params: {
xsjs: this.xsjs,
Shop: this.shoppingId,
PeriodoDe: this.periodOf,
PeriodoAte: this.periodUntil,
Kpi: this.kpi,
Select: this.select
},
});
}
async getResponseAxios(){
return await this.configAxios('https://www.apiUrl.com/');
}
}
module.exports = () => { return ApiDAO };
或者,如果您想使用axios create,则可以在前面将URL作为选项字段传递。我不认为其他参数有误。