【发布时间】:2017-09-22 01:08:53
【问题描述】:
我制作了最简单的 Pedestal 项目并在我的本地 repl 中运行它。但是,在localhost:8890 检查浏览器后,我看到了 �(替换字符)而不是实际文本(西里尔符号),我输入了我的基座路线。
我还检查了浏览器 devtools 响应标头: Content-Type:text/html;charset=utf-8 存在。
在你问之前:
- 是的,我设置了
charset=UTF-8作为响应,您可以在下面的代码中看到。 - 我的
core.clj文件也是UTF-8 编码。 - 我也试过其他浏览器,同样的。
- 附加信息:我使用的是 Windows,但在使用其他库和框架(ring、yada)之前从未遇到过这个问题。会不会是 Pedestal 在将我的代码传递给码头服务器时以某种方式在内部破坏了我的代码?我不知道。
项目的全部代码:
(ns samplepedestal.core
(:require [io.pedestal.http :as http]
[io.pedestal.http.route :as route])
(:gen-class))
(defn html-response
[req]
{:status 200
:body "<html lang=\"ru\">
<head>
<meta charset=\"utf-8\" />
<title>Текст на русском</title>
</head>
<body>Текст на русском</body>
</html>"
:headers {"Content-Type" "text/html; charset=UTF-8"}})
(def routes
(route/expand-routes
[[["/" {:get `html-response}]]]))
(def service-map
{::http/routes routes
::http/type :jetty
::http/port 8890})
(defn start []
(http/start (http/create-server service-map)))
;; -- Interactive development
(defonce server (atom nil))
(defn start-dev []
(reset! server
(http/start (http/create-server
(assoc service-map
::http/join? false)))))
(defn stop-dev []
(http/stop @server))
(defn restart []
(stop-dev)
(start-dev))
;; ---
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!"))
这是一种奇怪的行为,我不知道我错过了什么,所以任何帮助将不胜感激。谢谢!
【问题讨论】:
-
您确定您的 clj 文件是 utf-8 编码的吗?尝试写
\u0421而不是с(或其他类似的替代)。如果服务器正在发送正确的标头,那么您很可能从磁盘中读取了错误的字符。或者,您的浏览器使用的字体可能没有这些字符的字形? -
@amalloy,是的,我对 utf-8 编码的 clj 文件持肯定态度。我尝试写
\u0421而不是с- 没有用,得到了相同的结果。字体很好,我将相同的html代码粘贴到本地html文件中并打开它,它显示正确。 -
我在我的电脑上运行了你的代码,我的浏览器正确显示了西里尔符号。
-
@AlbertP,好吧,我为你感到高兴,但问题仍然存在。有什么想法吗?
标签: encoding utf-8 clojure jetty pedestal