Axios: Make AJAX POST Requests

10/10/2019 5:41:47 PM

Below are multiple ways to make a POST request with the Axios AJAX library.


function function__axios__post_1()
{
    var url = "/api/home/posttoserver/";


    axios.post(url,
        {
            id: 123,
            name: "Superman"
        })
        .then(function (response)
        {
            console.log(response);

            var result = response.data;
            console.log(result);
        })
        .catch(function (error)
        {
            console.log(error);
        });
}


function function__axios__post_2()
{
    var url = "/api/home/posttoserver/";
    var data = {
        id: 123,
        name: "Superman"
    };


    axios.post(url, data)
        .then(function (response)
        {
            console.log(response);

            var result = response.data;
            console.log(result);
        })
        .catch(function (error)
        {
            console.log(error);
        });
}


function function__axios__post_3()
{
    var url = "/api/home/posttoserver/";

    var axiosConfig =
    {
        method: "post",
        url: url,
        data: {
            id: 123,
            name: "Superman"
        }
    };


    axios(axiosConfig)
        .then(function (response)
        {
            console.log(response);

            var result = response.data;
            console.log(result);
        })
        .catch(function (error)
        {
            console.log(error);
        });
}