cors错误时,在节点JS pug模板中包含脚本时

发布于 2025-01-23 11:06:27 字数 1239 浏览 2 评论 0原文

我有一台Express Server,该服务器将PUG用作模板引擎。我正在尝试使用CDN添加尾风CSS,但我会收到这样的CORS错误:

错误:'https://cdn.tailwindcss.com/'来自Origin'http:// localhost:3000'已被CORS策略阻止:no'cescess-control-wallo-allow-origin'header'http:// localhost:3000'header

错误:访问脚本是在请求的资源上存在的。

在我的哈哈模板中,这是我的基本文件,我尝试包括脚本:

html
  head
    block head
      meta(charset='UTF-8')
      meta(name='viewport' content='width=device-width, initial-scale=1.0')
      link(rel='stylesheet' href='/css/style.css')
      link(rel='shortcut icon' type='image/png' href='/img/favicon.png')
      link(rel='stylesheet' href='https://fonts.googleapis.com/css?family=Lato:300,300i,700')
      title A & N Tours | #{title}
  
  body
    // HEADER
    include _header
        
    // CONTENT
    block content
      h1 This is a placeholder heading
      
    // FOOTER
    include _footer
    
    script(src="https://js.stripe.com/v3/" crossorigin="") 
    script(src='/js/bundle.js')
    script(src="https://cdn.tailwindcss.com" crossorigin="" defer) 

我的路由处理程序功能:

  const tours = await Tour.find().limit(3);
  res.status(200).render('homepage', {
    title: 'Home',
    topTours: tours
  })
});

I have an express server which uses the PUG as a template engine. I am trying to add tailwind CSS using the cdn but I'm getting a cors error like this:

Error: Access to script at 'https://cdn.tailwindcss.com/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

In my pug template this is my base file where I m trying to include the script:

html
  head
    block head
      meta(charset='UTF-8')
      meta(name='viewport' content='width=device-width, initial-scale=1.0')
      link(rel='stylesheet' href='/css/style.css')
      link(rel='shortcut icon' type='image/png' href='/img/favicon.png')
      link(rel='stylesheet' href='https://fonts.googleapis.com/css?family=Lato:300,300i,700')
      title A & N Tours | #{title}
  
  body
    // HEADER
    include _header
        
    // CONTENT
    block content
      h1 This is a placeholder heading
      
    // FOOTER
    include _footer
    
    script(src="https://js.stripe.com/v3/" crossorigin="") 
    script(src='/js/bundle.js')
    script(src="https://cdn.tailwindcss.com" crossorigin="" defer) 

My route handler function:

  const tours = await Tour.find().limit(3);
  res.status(200).render('homepage', {
    title: 'Home',
    topTours: tours
  })
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2025-01-30 11:06:27

您从TailWindcss.com请求的资源不带CORS标题。但是因为 crossorigin = crossorigin =“属性,浏览器期望它们。

如果您忽略此属性,问题就消失了,

script(src="https://cdn.tailwindcss.com" defer) 

您是否有设置属性的特殊原因?

以下HTML页面加载没有问题:

<!DOCTYPE html><html>
<head>
  <script type="text/javascript" src="https://cdn.tailwindcss.com" defer>
  </script>
</head></html>

The resource that you request from tailwindcss.com does not come with CORS headers. But because of the crossorigin="" attribute, the browser expects them.

The problem goes away if you omit this attribute

script(src="https://cdn.tailwindcss.com" defer) 

Is there a particular reason why you set the attribute?

The following HTML page loads without problems:

<!DOCTYPE html><html>
<head>
  <script type="text/javascript" src="https://cdn.tailwindcss.com" defer>
  </script>
</head></html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文