首页 > 解决方案 > 如何使用从 AJAX 获取 API 调用的 JSON 结果的参数调用另一个函数

问题描述

我必须使用 JSON 结果从此函数中获取 ParentId,然后在此函数之后调用下面给出的另一个函数:

public fetchLibraryDatafromSharePointList(clientID:string) {
  debugger;
  const reactHandler = this;
  jquery.ajax({  
    url: `${this.props.siteurl}/_api/web/lists/getbytitle('MSAs')/items?$filter=ClientID eq '${clientID}'&$orderby=Modified desc`,
    type: "GET",  
    headers:{'Accept': 'application/json; odata=verbose;'},  
    success: function(resultData2) {
reactHandler.setState({  
        items: resultData2.d.results  
      });  
    },  
    error : function(jqXHR, textStatus, errorThrown) {  
    }  
});  
}

以下是上述 API 成功后必须调用的函数,参数来自上一次调用的 JSON 结果:-

SetState 由 reactHandler 变量完成

public fetchDatafromSharePointList(ParentID) {
 debugger;
  const reactHandler = this;
  jquery.ajax({ 
url:`${this.props.siteurl}/_api/web/lists/getbytitle('MSASummaries')/items? 
    $filter=Parent eq ${ParentID}$top=1&$orderby=Modified desc`,  
        type: "GET",  
        headers:{'Accept': 'application/json; odata=verbose;'},  
        success: function(resultData) {  
          /*resultData.d.results;*/  
           reactHandler.setState({  
            items: resultData.d.results  
          });  
        },  
        error : function(jqXHR, textStatus, errorThrown) {  
        }  
    });  
    }

标签: ajaxreactjsspfx

解决方案


fetchLibraryDatafromSharePointList(clientID:string)您可以在更新状态后的成功响应中调用另一个函数。IE:

reactHandler.setState({  
    items: resultData2.d.results  
  }, ()=>{
     fetchDatafromSharePointList(your_parent_id_goes_here);
  }); 

推荐阅读