提问者:小点点

如何将多个源加载到HTML表中


这是我的密码。 它适用于一个源,但当我尝试加载第二个url时,它就不像我想要的那样工作了。

$(function(){
    window.addEventListener('load', function(){
        var fullUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('EmployeeInfo')/items?$select=Title,Age,Position,Office,Education,Degree";
        var fullUrl1 = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Employee2')/items?$select=Title,Age,Position,Office,Education,Degree";
            $.ajax({
              url: fullUrl,
              type: "GET",
              headers: {
                  "accept":"application/json; odata=verbose"
              }, 
              success: onSuccess,
              error: onError
            });
            $.ajax({
              url: fullUrl1,
              type: "GET",
              headers: {
              "accept": "application/json; odata=verbose"
              },
              success: onSuccess,
              error: onError
            });

共1个答案

匿名用户

然后使用一个Promise函数来加载数据,您可以根据需要使用任意多个URL,只需确保正确地连接它们即可。

此处:

window.addEventListener("load", function () {
  Promise.all([
    loadData("EmployeeInfo", _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('EmployeeInfo')/items?$select=Title,Age,Position,Office,Education,Degree"),
    loadData("Employee2", _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Employee2')/items?$select=Title,Age,Position,Office,Education,Degree"),
  ])
    .then(([r1, r2]) => {
      const objItems = r1.concat(r2);
      var tableContent =
        '<table id="employeeTab" style="width:100%" border="1 px"><thead><tr><td><strong>Name</strong></td>' +
        "<td><strong>Age</strong></td>" +
        "<td><strong>Position</strong></td>" +
        "<td><strong>Office</strong></td>" +
        "<td><strong>Education</strong></td>" +
        "<td><strong>Degree</strong></td>" +
        "<td><strong>Source</strong></td>" +
        "</tr></thead><tbody>";