masiboo
masiboo

Reputation: 4719

how to handle React axios how to handle net::ERR_CONNECTION_REFUSED

It was asked before, but if I follow the solution still in the console there is an uncouth error:-

    getExchangeAmount(){
  var url = 'http://localhost:8080/excurrency?currency='+this.state.currency+
            '&exCurrency='+this.state.excurrency+'&amount='+this.state.amount 

    axios.get(url)
    .then(response => {
        // success
        this.setState({result: response.data})
    })
    .catch((error) => {
        // handle this error
        console.log('error: '+error);
    })
}

in the console:-

GET http://localhost:8080/excurrency?currency=EGP&exCurrency=EGP&amount=1 net::ERR_CONNECTION_REFUSED

The above is in red which is an error. how to avoid or catch this?

Upvotes: 1

Views: 3791

Answers (2)

Funny Side
Funny Side

Reputation: 36

Try Like this

getExchangeAmount(e){
e.preventDefault();
  var url = `/excurrency?currency${this.state.currency}&exCurrency=${this.state.excurrency}&amount=${this.state.amount}`

    axios.get(url)
    .then(response => {
        // success
        this.setState({result: response.data})
    })
    .catch((error) => {
        // handle this error
        console.log('error: '+error);
    })
}

Upvotes: -1

mohamed ibrahim
mohamed ibrahim

Reputation: 583

if you are using a node server so install cors package on the server side:

npm i cors --save 

in index.js of server side:

const cors= require('cors');
app.use(cors());

then your code shall work..

if you are not using a node server so try this:

    getExchangeAmount(){
  var url = '/excurrency?currency='+this.state.currency+
            '&exCurrency='+this.state.excurrency+'&amount='+this.state.amount 

    axios.get(url)
    .then(response => {
        // success
        this.setState({result: response.data})
    })
    .catch((error) => {
        // handle this error
        console.log('error: '+error);
    })
}

Upvotes: 2

Related Questions