Is there a way to overcome the 404 error not found due to CORS when trying to update or create an excel file with Graph API?

  Kiến thức lập trình

i’m quite new to the programming world so sorry for my language and in a dire need of help.
I’m trying to use the Microsoft Graph API to update or create an excel file on OneDrive with a full stack app, a node express server written in javascript as backend and a Vue.js component as a frontend: the code works perfectly when used in local without any api but when I add the HTTP request to the API everything crashed and I receive a 404 error not found even if the file exists. Looking around, I’ve found that the main issue may be the CORS policy and I’ve tried a lot of things to overcome this issue but without luck.

This is my generateExcel function

    async generateExcel() {
  if (!this.inputFileName) {
    alert('Per favore inserisci il nome del file.');
    return;
  }

  if (!this.validateInput()) {
    alert('Per favore inserisci solo numeri nei campi dati.');
    return;
  }

  const data = [this.ICCID, this.IMSI, this.MSISDN, this.IMEI, this.COMPANY];
  const fileName = this.inputFileName.includes('.xlsx') ? this.inputFileName : `${this.inputFileName}.xlsx`;

  // parte di codice che effettua la richiesta API per caricare o aggiornare il file su OneDrive, 
  try {
    const response = await fetch(`https://graph.microsoft.com/v1.0/me/drive/root:/Test/${fileName}:/content`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/octet-stream',
        'Authorization': 'Bearer ' + this.accessToken, 
      },
      body: data,
    });

    if (!response.ok) {
      throw new Error('Network response was not ok');
    }

    alert("File aggiornato con successo.");
  } catch (error) {
    console.error('Errore durante l'aggiornamento del file:', error);
    alert("Errore durante l'aggiornamento del file.");
  }
    },

Do you have some tips to share?
Have a great day and keep going!

Charlie

Tried with fetch, axios, app.use(cors({
origin: ‘http://localhost:8080’
}));
Also changed browser and used extensions for Cors

New contributor

Charlie Mercury is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

LEAVE A COMMENT