axios: Axios Post Works but Responds with Network Error
I searched for this here, around the internet and I went and signed up in the chat but no one’s there. No response in a day.
Summary
I’m learning so excuse my newb code. I’m using async functions with axios that all work except for one. This one did work before as I never saw this error come up when I implemented it. The updateItem(item)
method is the one giving an error. What’s weird is that I was using axios.put
no problem but was getting an invalid verb error. Switched to axios.post
and it does work, it hits the method and sends the item and when the method is finished an Ok(200) response is returned. But an error is thrown on the axios.post
call of a network error. I’ll show the code and error below.
async function deleteSelectedItemsAsync(removedItems) {
try {
let response = await axios.post('/Items/DeleteItems', removedItems);
console.log('Delete response: ' + response.status);
} catch (e) {
console.log(e);
}
}
async function updateItem(item) {
var response;
try {
item.editButtonMessage = "Edit";
response = await axios.post('/Items/UpdateItems', item);
} catch (e) {
console.log(response);
console.log(e);
}
}
async function addItemToDatabaseAsync(item) {
try {
const response = await axios.post('/Items/PostItem', item);
} catch (e) {
alert(e);
}
}
Context
- axios version: v0.18.0
- Environment: Windows 10, Edge 41.16299.666.0, Chrome Dev 71.0.3573.0 (Official Build) dev (64-bit), and Regular Chrome latest version
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 39 (1 by maintainers)
Having the same issue. Postman does work, axios doesn’t
This sounds a lot like a mixture of errors. I doubt the error lies within Axios though from what I have read. If someone can give an example where it is shown to be a definitive Axios issue please open a new ticket for that issue.
I have same problem It works in Postman, but not when a request is sent to axios.
axios version
^0.19.2
In my case, after adding the cors in server, at the php code, I’ve made this error disappear. Here is the different language system to enable cors - https://enable-cors.org/server.html
@Bilaltcpaas I was having this issue using Mockoon adding the headers defined above resolved the issue for me, thank you.
I was getting Network Error when making an API request using AXIOS only in Window Server Firefox.Following are the steps to fix the issue.I am using NodeJS server
1- npm install cors
2-const cors = require(“cors”);
3- app.use(cors());
4- app.use(function(req, res, next) { res.setHeader(“Access-Control-Allow-Origin”, “*”); res.setHeader(“Access-Control-Allow-Credentials”, “true”); res.setHeader( “Access-Control-Allow-Methods”, “GET,HEAD,OPTIONS,POST,PUT,DELETE” ); res.setHeader( “Access-Control-Allow-Headers”, “Origin,Cache-Control,Accept,X-Access-Token ,X-Requested-With, Content-Type, Access-Control-Request-Method” ); if (req.method === “OPTIONS”) { return res.status(200).end(); } next(); });
Same issue here!!
Any luck?