Search code examples
javascriptaxiosfetch

cant load axios by using cdn


I'm trying to use axios by cdn

but i'm getting this error : Refused to load the script 'https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.


Solution

  • In my case loading axios cdn through pug base template, I created an express middleware which sets header of Content-Security-Policy.

    app.use(function(req, res, next) { 
      res.setHeader( 'Content-Security-Policy', "script-src 'self' https://cdnjs.cloudflare.com" ); 
      next(); 
    })