Global Axios Get JSON Method

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 => {}
Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.