I'm trying to run the following command in a React component after clicking a button:
import axios from 'axios'
export function callApi(index, callback) {
axios.get('http://localhost:8080/search?index=120&json=true&all=true')
.then(res => {
alert(res)
})
.catch((error) => {
alert("error: " + error)
})
}
It works fine when I'm running the code in node,
var axios = require('axios')
axios.get('http://localhost:8080/search?index=120&json=true&all=true')
.then(res => {
alert(res)
})
.catch((error) => {
alert("error: " + error)
})
But when I call it after clicking a button, I get an alert that says "Error: Network Error." I've tried running it with fetch() instead with no CORS either, but I'm getting an empty response instead of one with data.
fetch('http://192.168.1.156:8080/search?index=120&json=true&all=true', {mode: 'no-cors'})
.then(res => {
alert(res)
})
.catch((error) => {
alert("error: " + error)
})
I've tried running this with other APIs like api.github.com but I get the same result. It works in node, but not React.