簡體   English   中英

從兩個 URL 獲取“GET”請求?

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

我在下面提供的代碼可以成功地從 SharePoint 站點的某個位置提取列表項,然后將它們填充到 div 表中。 我沒有包含該代碼,因為它不相關。

在顯示的第一個 function 下有getbytitle API 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');
   }
  });
});

下面我找到了解決方案。 將 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