我正在尝试使用axios创建发布请求,并在正文上发送数据,但是当我在后端获取它时,正文始终为空。我不知道出什么问题了,我正在按照axios文档中的要求发送数据。如果有人可以为此提供帮助,我将不胜感激。
这是我的请求功能:
import axios from 'axios';
const request = async ({
method,
action,
headers,
data,
callbackFunction,
exceptionHandlerFunction,
}) => {
const config = {
method: method || 'get',
baseURL: `http://localhost:3333/`,
url: action,
data: data || null,
headers: {
'Content-Type': 'application/json',
...headers,
},
};
const { data: response } = await axios
.create(config)
.request()
.catch(error => {
if (exceptionHandlerFunction) {
exceptionHandlerFunction();
} else {
console.log(error);
}
});
if (callbackFunction) {
callbackFunction(response);
} else {
alert({ message: response.meta.message });
}
};
export default request;
这是正在发出的请求:
import React from 'react';
import request from './request';
function App() {
const makeRequest = () => {
request({
method: 'post',
action: 'test/endpoint',
data: {
test: 'test',
},
callbackFunction: response => {
console.log(response);
},
});
};
return (
<div>
<button onClick={() => makeRequest()}>
test
</button>
</div>
);
}
export default App;