1

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

Clomez
  • 1,410
  • 3
  • 21
  • 41
  • when are you calling the function `showCurrent `? in your component? – ztadic91 Dec 03 '18 at 14:44
  • from handleClick which is triggered by onClick, but as stated it wont print console.log twice, so i have doupts about it being ShowCurrent being called twice. Console.log() placed in handleClick function will not be printed twice either. – Clomez Dec 03 '18 at 15:16

1 Answers1

0

In case anyone wonders here full of desparation. hear hear, it was all about proxy in my front-side client...

After few days of debugging (with very misleading error messages) i found a thread talking about how nginx sometimes re-tries fetch if there is Proxy present

proxy: {
        '/api': 'http://localhost:8080'
    }

After removing this, i got OPTIONS request, as expected.

next up, fetching a beer.

Clomez
  • 1,410
  • 3
  • 21
  • 41