问题
My axios POST request works about 70% of the time. There doesn't seem to be a pattern between which inputs work and which ones don't (length doesn't seem to matter). I've tried reading all of the stackoverflow questions with similar problems and nothing has worked.
onSubmit() {
const newInput = {
input_text: this.state.value,
}
instance.post('http://localhost:4000/inputs/add/', newInput)
.then(res => {
console.log(res.data);
})
.catch(error => {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
}
});
};
In this code snippet, "instance" is my axios instance which I've applied a timeout of 1000 on (just wanted to see if that would make a difference). I applied an axios interceptor on the request to try to understand more clearly what is going on, but behavior is identical with working and non-working requests. Meaning, "request was sent" is logged consistently meaning the request has sent, and the config logged looks similar between working and non working requests.
instance.interceptors.request.use(function (config) {
console.log("request was sent");
console.log(config);
return config;
}, function (error) {
console.log(error);
return Promise.reject(error);
});
What the config looks like:
{url: "http://localhost:4000/inputs/add/", method: "post", data: {…}, headers: {…}, transformRequest: Array(1), …}
adapter: ƒ xhrAdapter(config)
data: {input_text: "l"}
headers: {common: {…}, delete: {…}, get: {…}, head: {…}, post: {…}, …}
maxContentLength: -1
method: "post"
timeout: 1000
transformRequest: [ƒ]
transformResponse: [ƒ]
url: "http://localhost:4000/inputs/add/"
validateStatus: ƒ validateStatus(status)
xsrfCookieName: "XSRF-TOKEN"
xsrfHeaderName: "X-XSRF-TOKEN"
__proto__: Object
I am fairly sure that in non-working instances, the request doesn't even reach the backend. This is because in the below code snippet of my backend route, non-working instances log nothing while working instances log both "this worked" and also "finished".
inputRoutes.route('/add').post(function(req, res) {
req.body.input_output = processor.processText(req.body.input_text);
let input = new Input(req.body);
input.save()
.then(todo => {
res.status(200).json({'input': 'input added successfully'});
console.log("this worked");
})
.catch(err => {
res.status(400).send('adding new input failed');
console.log("this didn't work");
});
console.log("finished");
});
Strangely enough, the POST request actually started working more frequently once I put the interceptor in... FYI, the request works 100% of the time on Postman. None of the error catches I've implemented seem to be hit as well and nothing is logged.
I would highly appreciate if anyone had any suggestions either for further debugging methods to try, or if anyone knows a solution to this problem. I've put debuggers/console.log statements everywhere unfruitfully and am out of ideas. Thanks!
来源:https://stackoverflow.com/questions/58326291/axios-post-request-only-working-sometimes-mern-app