S
S
string152017-11-06 00:09:37
JavaScript
string15, 2017-11-06 00:09:37

Ajax not working in promises?

Hey! I study promises, everything seems to work as I need, but at the end the ajax request does not work!
I do not understand what's the matter?

export function getDocList(options) {
  // const url = 'data.json';
  const url =
    `http://192.168.235.188:9081/prototype/docList?clientName=${options.client}&Q=${options.period}&year=${options.year}`;

  return ((dispatch) => {
    dispatch({
      type: GET_DOCLIST_REQUEST,
      payload: 'Loading...'
    });

    axios.get(url, { crossdomain: true })
      .then((response) => {
        dispatch({
          type: GET_DOCLIST_SUCCESS,
          payload: response.data
        });
      })
      .catch((err) => {
        dispatch({
          type: GET_DOCLIST_FAILURE,
          payload: err
        });
      });
  });
}

//#################################################################################

   getDocs() {
    const dataObjForRequst = {
      client: this.props.clientIsChecked,
      year: this.props.yearIsChecked,
      period: this.props.periodIsChecked
    };

     this.props.receiveOnClick();
//Здесь в action происходит ajax запрос
     this.props.getdocList(dataObjForRequst);
  }

  changePeriodsOnPrev() {
    let promise = new Promise((resolve, reject) => {
      this.props.changePeriodsOnPrev();
      resolve("Работает!");
    });

    promise
      .then((result) => {
        this.getDocs();
    })
      .catch((err) => {
        console.log(err)
      });
  }


upd: Found a solution, swapped calls

//Здесь в action происходит ajax запрос
     this.props.getdocList(dataObjForRequst);
     this.props.receiveOnClick();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergealmazov, 2018-05-03
@sergealmazov

Your problem is in a fundamental understanding of how asynchronous operations work. Learn the basics, then you'll understand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question