0

I am new to this React.js axios concept and when I try to call axios.get, I get AxiosError: Network Error I'm not sure why this error is coming and I have read this and this some other examples also but I didn't get the proper solution.

And I have install the below two commands

npm install axios and npm install cors

package.json

Below is my code :-

import React from "react";
import { Button } from "@mui/material";
import axios from "axios";

function GetApiData() {

var data = {
    vendorname: "achintya",
      };
    
      const postData = () => {
        axios({
          method: "post",
          baseURL: "https://somesite.com",
          url: "/bkdb_getvendorcodes",
          payload: data,
        })
          .then(function (response) {
            // handle success
            console.log(response);
          })
          .catch(function (error) {
            // handle error
            console.log(error);//showing error here 
          });
      };
    
    return (
        <>
            <Button variant="outlined" onClick={postData}>POST data</Button>
        </>
    );
}

export default GetApiData;

When I run the above code I'm getting the below error

network error

But the service is working fine example img

service is working

I don't have much knowledge to fix this error please suggest me where I did the mistake and how to achieve this.

1 Answer 1

0

you need to allow cors origin in your backend server or you can use an alternative approach for development you can use below extension.

https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.