I've read multiple posts about similar problems, but I don't have CORS problem (i don't think) and my network tab does not show second post being send from client
I'm using Axios and react for my front-end client and after a long time of debugging I found out that something is triggering my RestController method, always twice, always about two minutes in (it's a very long response with a huge amount of data)
Network tab does not show this second POST
, but when ran from Postman, everything works as expected (no random double calls)
Axios:
showCurrent = (dateA, dateB) => {
axios.post("api/allData", {
params: {
date_a: dateA,
date_b: dateB
}})
//successful fetch
.then(res => this.setState({
Data: res.data,
isLoading: false,
showPage: true,
home: false,
}))
.catch(function (error) {
//handle errors here
alert(error.message);
})
};
and requestMapping:
@RequestMapping(value = "/allData", method = RequestMethod.POST)
public @ResponseBody String allData(@RequestBody String req) throws JsonProcessingException {
CDate cDate = parseJSON(req);
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(DateRange.getDataForDateRange(date));
}
Controller class also has @RestController
annotation
So if i ran query taking under ~2min it will be ok. but longer than that and the controller will be called twice and everything goes haywire.
I can add more code if needed.. just not sure how to investigate this.
EDIT:
Added Console.log in method doing the post in the client, and in server-side Java.
console.log("whaaat");
axios.post("api/allContracts", {
params: {
date_a: dateA,
date_b: dateB
}}).....
whaaat will only be run once, while the post will be run twice and the endpoint will trigger twice....
EDIT2:
What the..... I also added console.log()
to every step from click to fetch.
Nothing will trigger twice except for POST
...
even with reacts native fetch
same will occur
showCurrent = (dateA, dateB) => {
console.log("whaaat");
let obj = {};
let params = {};
params.date_a = dateA;
params.date_b = dateB;
obj.params = params;
this.setState({isLoading: true});
fetch("api/allData", {
method: 'POST',
body: JSON.stringify(obj),
mode: 'cors',
}).then( res => {
return res.json()
}).then( data => {
this.setState({
Data: data,
isLoading: false,
showPage: true,
home: false,
})
}).catch(error => {
//handle errors here
alert(error.message);
});
EDIT 3: Still no solution whatsoever.... I read about preflight OPTIONS request, but there is no such thing on network tab. I tried to handle OPTION request in different RequestMapping but that dosent even get triggered....
Tried to put timeout of about 2 million sec in both server and client but that dosent seem to do anything either.. Postman does not have this problem, everything works fine