Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

axios get request

import axios from "axios";
/// GET REQUEST
/// Example 1 : Start
// Simple Get Request using Axios
function getData() {
  return axios.get("url");
}
/// Example 1 : End
///
/// Example 2 : Start
// Get Request with Params
getData({ page: page, limit: 5, sort: "name", order: "ASC" })
      .then((res) => {
        // console.log(res);
        // op -> {data: Array(5), status: 200, statusText: "OK", headers: Object, config: Object…}
        // console.log(res.data);
        // op ->  [Object, Object, Object, Object, Object]
        setData(res.data);
      })
      .catch((err) => console.log(err));

function getData(params = {}) {
  // console.log(params); // op -> {page: 1, limit: 5, sort: "name", order: "ASC"}
  return axios.get("url", {
    params: {
      _page: params.page,
      _limit: params.limit,
      _sort: params.sort,
      _order: params.order
    }
  });
}
/// Example 2 : End

Source by flaviocopes.com #
 
PREVIOUS NEXT
Tagged: #axios #request
ADD COMMENT
Topic
Name
7+3 =