我在React-Redux项目中使用Axios进行GET请求,但出现以下错误:
TypeError: "config.method.toLowerCase is not a function"
request Axios.js:43
wrap bind.js:11
apiCall api.js:32
apiCall api.js:29
... ...
api.js是我自己项目中的文件。 bind.js和Axios.js来自Axios库。这是我的api函数:
export function apiCall(method, path, data){
let url = backendDomain + path
let config = {
method: [method],
url: [url],
data : [data],
headers:{
"Content-Type":"application/json",
"Accept":"application/json"
}
}
return new Promise((resolve, reject)=>{
return axios(config).then(res=> {
return resolve(res.data)
}).catch(err => {
return reject(err.response);
})
})
使用apiCall()的函数是此函数:
export function authUser(url, userData, method){
return (dispatch) => {
return new Promise((resolve, reject)=>{
return apiCall(method, "/"+`${url}`, userData)
.then((data) => {
...
resolve();
})
.catch(err=>{
...
reject();
})
})
}
}
您认为我的代码有问题还是库有问题?当我使用authUser调度操作(针对Redux State)时,我再次检查了“方法”是否为字符串,我在api.js中使用console.logged typeof方法,并返回了字符串。
编辑:
我尝试调用toString()传递给apiCall()的方法参数,但是没有用:
let reMethod = method.toString();
const config = {
method: [reMethod],
url: [url],
data : [data],
headers:{
"Content-Type":"application/json",
"Accept":"application/json"
}
}
答案 0 :(得分:2)
如我的评论中所述,当axios需要一个字符串时,您将提供一个数组:
const config = {
method: reMethod,
url: url,
data : data,
headers:{
"Content-Type":"application/json",
"Accept":"application/json"
}
}