如何使用 axios get 请求发送正文数据和标头?

14 get http reactjs axios

我试过了

axios.get(url, {headers:{},data:{}}) 
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

Kha*_*bir -2

你可以试试这个:

const getData = async () => {
    try {
        const response = await axios.post(`https://jsonplaceholder.typicode.com/posts`, {
            method: 'POST',
            body: JSON.stringify({
                id: id,
                title: 'title is here',
                body: 'body is here',
                userId: 1
            }),
            headers: {
                "Content-type": "application/json; charset=UTF-8"
            }
        })
            .then(response => response.json())
            .then(json => console.log(json));
        console.warn(response.data);
    } catch (error) {
        console.warn(error);
    }
}
Run Code Online (Sandbox Code Playgroud)