簡體   English   中英

electronjs用axios沒有反應

[英]Electronjs with axios no response

我剛開始學習 electronjs。 我在preload.js文件中添加了axios

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
});

我確實收到status:200作為響應,但沒有響應數據。 也沒有控制台消息。 我錯過了什么嗎? 但如果我在內容加載后執行請求,它會返回錯誤。

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();
});

上面的返回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. 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.

我在將圖像從外部 URL 加載到我的背景時遇到了同樣的問題。 在我的index.ts (electron 應用程序的入口點)中,我訂閱了"ready"事件並指定了這個responseHeaders *表示我們可以接受當前頁面需要加載的所有資源。 不建議使用*標志,但我只是為了測試才使用它。 更多: MDN

import { session, app } from "electron"

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM