【发布时间】:2019-12-17 15:28:40
【问题描述】:
我的页面使用 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));
}
);
};
}
【问题讨论】:
-
不应该是 then 和 catch 作为正常的 Promise 吗?
-
你能解释一下吗