简体   繁体   English

从两个 URL 获取“GET”请求?

[英]“GET” Request Pull from Two URLS?

The code I have provided below is successful for pulling list items from somewhere on a SharePoint site and then populating them in a div table.我在下面提供的代码可以成功地从 SharePoint 站点的某个位置提取列表项,然后将它们填充到 div 表中。 I did not include the code for that as it's not relevant.我没有包含该代码,因为它不相关。

Under the first function shown there is the getbytitle API URL.在显示的第一个 function 下有getbytitle API URL。 If I wanted to GET from another list with the same items, how would I call that URL?如果我想从另一个列表中获取相同的项目,我将如何调用 URL?

$(function(){
    $("#btnClick").click(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
            });



  function onSuccess(data) {
     var objItems = data.d.results;
     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>' +'</tr></thead><tbody>';
  
     for (var i = 0; i < objItems.length; i++) {
         tableContent += '<tr>';
         tableContent += '<td>' + objItems[i].Title  + '</td>';
         tableContent += '<td>' + objItems[i].Age + '</td>';
         tableContent += '<td>' + objItems[i].Position + '</td>';
         tableContent += '<td>' + objItems[i].Office + '</td>';
         tableContent += '<td>' + objItems[i].Education + '</td>';
         tableContent += '<td>' + objItems[i].Degree + '</td>';
         tableContent += '</tr>';
 }
   $('#employees').append(tableContent);
   }
    function onError(error) {
        alert('Error');
   }
  });
});

Below I found the solution.下面我找到了解决方案。 Load the urls into the request, and all you have to do is concatenate the response into a single array.将 url 加载到请求中,您所要做的就是将响应连接到一个数组中。

function loadData(source, url) {
  return fetch(url, { headers: { accept: "application/json; odata=verbose" } }) // make request
    .then((r) => {
      if (!r.ok) throw new Error("Failed: " + url);  // Check for errors
      return r.json();  // parse JSON
    })
    .then((data) => data.d.results) // unwrap to get results array
    .then((results) => {
      results.forEach((r) => (r.source = source)); // add source to each item
      return results;
    });
}

window.addEventListener("load", function () {
  Promise.all([
    loadData("AMMODeliverables", "_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Employee2')/items?$select=Title,Age,Position,Office,Education,Degree";"),
    loadData("DarQDeliverables", "_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('EmployeeInfo')/items?$select=Title,Age,Position,Office,Education,Degree";"),
    loadData("WTBnDeliverables", 
  ])
    .then(([r1, r2]) => {
      const objItems = r1.concat(r2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM