0
  • I am writing a basic JS function in Microsoft 365 Excel add-in generated using yeoman generator which I just want to fetch some data from a url, while fetching from https it is working fine.
  • But while doing the same with http I get an error saying TypeError: Failed to fetch. Now, I understand that I can't fetch from a http link but is there a way around.
  • Have included <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"> in the html file.
  • Code
// Fetch json value
      console.log(`Fetching`);
      var url = "http://1<ip>:<port>/fetch_api";
      //var url = "https://api.publicapis.org/entries";
      const response = await fetch(url, {
        method: 'GET',
        headers: {
          accept: 'application/json',
        },
      });

      if (!response.ok) {
        throw new Error(`Error! status: ${response.status}`);
      }

      const result = await response.json();
      console.log(result);
Lav Sharma
  • 327
  • 1
  • 5
  • 23

3 Answers3

0

Firstly you made mistype in headers dictionary headers: { 'Content-Type': 'application/json' } Then rememeber that this function have to be asynch.

  • Removed the unwanted code, after removing got error as ```taskpane.js:60 Mixed Content: The page at 'https://onedrive.live.com/' was loaded over HTTPS, but requested an insecure resource 'http://120.88.182.53:8086/Test_API/testapi'. This request has been blocked; the content must be served over HTTPS. TypeError: Failed to fetch``` – Lav Sharma Aug 10 '22 at 11:39
  • try approach from [here](https://stackoverflow.com/questions/52130918/web-api-error-this-request-has-been-blocked-the-content-must-be-served-over-h) – user18655467 Aug 10 '22 at 11:45
  • It is still giving the same error. – Lav Sharma Aug 10 '22 at 11:49
0

no need to write that much code to get requests using fetch. get request is the default method. you can write just fetch(URL). for another type of requests you should write method, headers, body, etc.

just write this function and invoke it.

async function getData () {
const URL = 'your_url'
let response = await fetch(URL)
let data = await response.json()
console.log(data)
}

I am using an async await for fetching data. for any type of promise request you should use async await, this will help you to handle promises.

  • Removed the unwanted code, after removing got error as ```taskpane.js:60 Mixed Content: The page at 'https://onedrive.live.com/' was loaded over HTTPS, but requested an insecure resource 'http://120.88.182.53:8086/Test_API/testapi'. This request has been blocked; the content must be served over HTTPS. TypeError: Failed to fetch``` – Lav Sharma Aug 10 '22 at 11:40
  • It seems like you're requesting any secure server using the localhost server, which is not giving permission to you, try installing the [cors extension](https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en) once – Akshay Kumar Aug 10 '22 at 11:53
  • That is not working for ```Microsoft 365 Excel``` – Lav Sharma Aug 12 '22 at 05:25
0
    fetch('...')
        .then(res => res.json())
        .then(data => {
         const table = document.querySelector('table '); 
         const sortedData = data.slice(0, 5).sort((a, b) => b.id - a.id);
            
         sortedData.forEach(user => {
                
         const username = user.username === "Bret" ? "BBBBB" : user.username;
    
                const markup = `
                    <tr> 
                        <td>${user.id}</td>
                        <td>${user.name}</td>
                        <td>${username}</td> <!-- Koristi promijenjeno korisničko ime -->
                        <td>${user.email}</td>
                        <td>${user.address.street}</td>
                        
                    </tr>
                `;



table.insertAdjacentHTML('beforeend', markup);});
        });
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 23 '23 at 08:23