如何将多个源加载到 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

            });


紫衣仙女
浏览 68回答 1
1回答

catspeake

然后使用 Promise 函数加载数据,您可以根据需要使用任意数量的 url,只需确保正确连接它们即可。这里:window.addEventListener("load", function () {&nbsp; Promise.all([&nbsp; &nbsp; loadData("EmployeeInfo", _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('EmployeeInfo')/items?$select=Title,Age,Position,Office,Education,Degree"),&nbsp; &nbsp; loadData("Employee2", _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Employee2')/items?$select=Title,Age,Position,Office,Education,Degree"),&nbsp; ])&nbsp; &nbsp; .then(([r1, r2]) => {&nbsp; &nbsp; &nbsp; const objItems = r1.concat(r2);&nbsp; &nbsp; &nbsp; var tableContent =&nbsp; &nbsp; &nbsp; &nbsp; '<table id="employeeTab" style="width:100%" border="1 px"><thead><tr><td><strong>Name</strong></td>' +&nbsp; &nbsp; &nbsp; &nbsp; "<td><strong>Age</strong></td>" +&nbsp; &nbsp; &nbsp; &nbsp; "<td><strong>Position</strong></td>" +&nbsp; &nbsp; &nbsp; &nbsp; "<td><strong>Office</strong></td>" +&nbsp; &nbsp; &nbsp; &nbsp; "<td><strong>Education</strong></td>" +&nbsp; &nbsp; &nbsp; &nbsp; "<td><strong>Degree</strong></td>" +&nbsp; &nbsp; &nbsp; &nbsp; "<td><strong>Source</strong></td>" +&nbsp; &nbsp; &nbsp; &nbsp; "</tr></thead><tbody>";
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript