0

I just started learning electronjs. I have added axios in my preload.js file

const axios = require('axios'); 

axios.get('https://jsonplaceholder.typicode.com/todos/1')
.then(function (response) {
  // handle success
  console.log(response);
})
.catch(function (error) {
  // handle error
  console.log(error);
})
.then(function () {
  // always executed
});

I do get a status:200 as a response, but there is no response data. there are also no console messages. Am i missing something? But if I do the request after the content load, it returns an error.

const axios = require('axios'); 

let getData = () => {
  axios.get('https://jsonplaceholder.typicode.com/todos/1')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });
}

window.addEventListener('DOMContentLoaded', () => {
  getData();
});

the above returns Refused to connect to 'https://jsonplaceholder.typicode.com/todos/1' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.

Phil
  • 157,677
  • 23
  • 242
  • 245
roullie
  • 2,830
  • 16
  • 26
  • Does this answer your question? [ContentSecurityPolicy Preventing fetch request in Electron](https://stackoverflow.com/questions/69790650/contentsecuritypolicy-preventing-fetch-request-in-electron) – Phil Mar 30 '22 at 05:46
  • When you say there is no response data, is the `data` property of the response object undefined? https://axios-http.com/docs/res_schema – Nick Bailey Mar 30 '22 at 05:51
  • @Phil Thanks, i will will take a look at it. – roullie Mar 30 '22 at 05:54
  • @NickBailey Thank you for commenting. as i have said also above.. there are no console messages. looking at the `network` tab.. the `preview` is blank – roullie Mar 30 '22 at 05:55

1 Answers1

0

I had same issue with loading image from external URL to my background. In my index.ts (entry point of electron app) I subscribe to "ready" event and specify this responseHeaders. * means that we can accept all resources that need to be load for current page. It's not recommended to use * flag, but just for testing I use it. More: MDN

import { session, app } from "electron"

app.on("ready", () => {
  session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
    callback({
      responseHeaders: {
        ...details.responseHeaders,
        "Content-Security-Policy": ["*"],
      },
    });
  });
})
Secre
  • 1