我的页面使用的是ajax请求,但我们将它们移到了axios。我已经更改了代码,但是响应数据是空的。
这是我以前的代码:
export function getMList(params, onSuccess, onFailure) {
const url = `/admin/list`;
const {
name,
type
} = params;
return $.ajax({
type: 'GET',
url,
processData: false,
contentType: 'application/x-www-form-urlencoded',
data: $.param({name, type}),
success: (response) => {
onSuccess(response);
},
error: (error) => {
onFailure(error);
}
});
}现在,将其更改为axios后,它是:
export function getMList(params) {
const {
name,
type
} = params;
axios.get(`/admin/list`,params,{
headers : {'contentType': 'application/x-www-form-urlencoded'
}
}).then((res) => { return res; }, (err) => { return err; })
}我做错了什么。是我作为参数传递的数据吗?
查询的用法如下:
export function getMList(id, name, type) {
const encodedName = encodeURI(name);
return (dispatch) => {
dispatch(requestMList({ id }));
admin.getMList({ name: encodedName, type },
(response) => {
dispatch(receivedMList({ id, name, type, response }));
},
(error) => {
dispatch(failedMList(id));
}
);
};
}发布于 2019-08-10 07:48:26
get等axios请求方法接受两个参数,一个是请求url,一个是axios对象。在config对象中,您将需要data键来设置请求主体(您的params)和headers键(您的内容类型)。
export const getMList = (params) => {
return axios.get('/admin/list', {
data: params,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
}由于axios返回一个Promise,而上面的函数返回axios请求,因此您可以使用.then和.catch链接“成功”和“失败”逻辑,而不是将它们作为回调传递。
admin
.getMList({ name: encodedName, type })
.then((response) => dispatch(receivedMList({ id, name, type, response })))
.catch((err) => dispatch(failedMList(id, err)));https://stackoverflow.com/questions/57437669
复制相似问题