script-lab
script-lab copied to clipboard
Not allowed to request resource
Bug Report
- Host: Excel
- OS: macOS Big Sur 11.3
- Browser: Microsoft Excel for Mac (v 16.54)
- Environment: Production
Expected behavior:
Expected fetch
to request CSV file over the internet.
Are there limitations for fetch that are not documented?
Actual behavior:
Returns "Not allowed to request" resource.
Steps to Reproduce:
Use the following code to replicate the behaviour:
async function createMSCI() {
await Excel.run(async (context) => {
const response = await fetch('http://www.sca.isr.umich.edu/files/tbmics.csv', {
headers: {
'Content-Type': 'text/csv'
},
responseType: 'blob'
});
if (!response.ok) {
throw Error("Response is not in OK state");
}
const data = response.blob();
console.log(data);
});
}
I think the problem is related to the server settings. I uploaded the csv to my azure storage, and the first time, it also failed. And I set the cors to allow any origin access, and it works fine. You can try below sample code.
$("#run").click(() => tryCatch(run));
async function run() {
await Excel.run(async (context) => {
await fetch('https://chenxizhang.blob.core.windows.net/public/tbmics.csv', {
}).then(x=>x.blob()).then(x=>console.log(x.size));
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
This is a great question for Stack Overflow.