从功能参数向axios http请求发送数据

时间:2020-01-21 17:47:28

标签: axios

let name = "myname"
function updateTodo(name) {
    axios.put('https://jsonplaceholder.typicode.com/todos/1', {
        title: name,
        completed:true
    })
    .then(res=>showOutput(res))
    .catch(err=>console.log(err))
}

我只是想将名称变量作为函数参数发送到标题数据,这表明我正在发送有关如何解决此问题的空数据。

1 个答案:

答案 0 :(得分:0)

如果要在HTTP请求正文中发送JSON,请记住将标头Content-Type设置为application/json

例如:

const body = {
    title: name,
    completed: true
};

axios.put('https://jsonplaceholder.typicode.com/todos/1', body,  {
    headers: {
        'Content-Type': 'application/json'
    }
});

希望有帮助。 享受吧!