我如何通过React在onclick函数中使用Ajax?

时间:2018-11-05 11:24:56

标签: ajax reactjs function onclick

我想通过使用ajax在此函数中显示一些结果。使用React时,如何在onclick函数中使用Ajax?

onItemClick(a) {
  console.log("Button was clicked!");
  var desc = Flightdesc.info.departureinformation.routeshow.description;
  $.ajax({
    url: "/flyrules.inc",
    type: "post",
    data: {
      id: desc
    },
    success: result => {
      console.log(result);
    }
  });
}

2 个答案:

答案 0 :(得分:0)

适用于您的代码的预防措施很少,我建议您阅读官方网站上的React演练。

// Bind function to local context with fat arrow
onItemClick = (a) => {
  // Prefer const/let over var
  const desc = Flightdesc.info.departureinformation.routeshow.description;
  $.ajax({
    url: "/flyrules.inc",
    type: "post",
    data: {
      id: desc
    },
    success: result => {
      // setState is an ASYNC function so check the new state in the callback
      this.setState({ data: result }, () => console.log(this.state.data););
    }
  });
}

答案 1 :(得分:0)

您应该使用不需要任何库的fkprofessional API。

fkpreference