【问题标题】:compojure/clojure GET route with CSS gives plain text stylesheet responsecompojure/clojure GET route with CSS 提供纯文本样式表响应
【发布时间】:2013-06-10 10:33:23
【问题描述】:

这是我的代码,它具有溢出简单样式表的功能:

(defroutes app-routes (GET "/style.css" [] (my-css-function)))
(defn my-css-function [] "(css strings...)")

当我在浏览器中输入“/style.css”时,它会溢出无法被 html 文件使用的纯文本字符串,同时在语法上是正确的。我猜 compojure 弄乱了上下文类型,以至于浏览器将我的字符串解释为奇怪的东西,而不是普通的 css 文件?

【问题讨论】:

  • 当您“查看源代码”时,您在浏览器中看到了什么?

标签: clojure leiningen compojure


【解决方案1】:

my-css-function 返回一个字符串,compojure 作为响应的内容返回,如果没有明确指定内容类型,那么我的猜测是 text/plaintext/html 是用过。

如果要为响应指定 text/css 内容类型,可以通过以下方式进行:

(defn my-css-function [] 
  {:headers {"Content-Type" "text/css"}
   :body "body { background-color: #CCC; }"})

【讨论】:

    【解决方案2】:

    您需要修改您的代码以包含以下内容:

      (route/resources "/") ; special route for serving static files like css
                            ; default root directory is resources/public/
    

    您可以查看this answer了解更多详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      相关资源
      最近更新 更多