Skip to content

Global Axios Get JSON Method

By Jasper Frumau

Basic Axios get request to load date as JSON data is done with relative ease using a basic call, naming it get with added url and possible parameters like id of post, page or menu to load:

axios
  .get('/path/to/call/' + params, {
    responseType: "json",
  })
  .then(function (response) {
    console.log(response);
  });

NB here outputting to console for this example

You could however also set up a global method for it using

window.axios.getJSON = function(url, params)
{
    var request = {
        method: 'get',
        responseType: 'json',
        url: url,
        params: params
    };

    return this(request);
};

which you can then use cross your entire application with calls like

axios.getJSON('/app/some-module/' + args.id)
    .then(res => {}