Home > Mobile >  Axios get with params not working on frontend, postman working
Axios get with params not working on frontend, postman working

Time:01-21

I'm trying to get som data from database, on frontend I'm using axios and my request is:

  const getAllPayments = async () =>

    {
      const { data } = await Axios.get("payment_query/allPayments.php", {
        params: {
          company_id: 1,
          year: 2022,
          start_mounth: 1,
          end_mounth: 12,
        },
      });
      if (data) {
        console.log(data);
        setPayments(data);
        setWait(false);
        return true;
      }
      setPayments([]);
    };

When I try to send request via postman everything working here is json body:

{
      "company_id": 1,
      "year" : 2022,
     "start_mounth" : 1,
     "end_mounth" : 12
}

Can anyone think of what could be wrong with that?

CodePudding user response:

GET requests don't work with body, they work with search params. Check to see if Axios is using POST for it's request or make sure your endpoint is expecting a get request and not a POST request.

CodePudding user response:

It's possible that the issue is with how the params are being passed in the axios request on the frontend. In the code you provided, the params are being passed as an object, but in the Postman example, they are being passed in the body of the request as JSON. It's also possible that there's a CORS issue, you can try to add the headers in the server side:

'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',

Another thing you can do is to check the browser's console for any error messages that may provide more information about the issue.

  • Related