我正在尝试制定代码,这将允许我在 html 输入字段中打印一些内容,然后 Haskell 代码将返回我在下一个 div 中打印的内容(从长远来看,这是一个 Ajax 搜索引擎)。两者之间的联系似乎已经建立,但以一种非常奇怪的方式。不仅如此,我不太明白如何按以下顺序为 queryString req 制作案例。这是 Haskell 代码:
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.Wai.Handler.Warp
import Network.HTTP.Types
import Data.ByteString.Lazy
main :: IO ()
main = run 3000 app
app :: Application
app req resp = do
case pathInfo req of
["main"] -> resp $ responseFile status200 [("Content-Type","text/html")] "search.html" Nothing
_ -> resp $ responseLBS status404 [("Content-Type","text/plain")] "No such file."
case queryString req of
[("q=",Just stuff)] -> resp $ responseLBS
status200
[("Content-Type","text/html")]
(fromStrict stuff)
[("q=",Just "")] -> resp $ responseLBS
status200
[("Content-Type","text/html")]
""
_ -> resp $ responseLBS
status404
[("Content-Type","text/html")]
"sorry"
这是我的搜索页面:
<!DOCTYPE html>
<html>
<script>
function getStuff(str) {
if (str.length == 0) {
document.getElementById("results").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("results").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "http://localhost:3000/main?q=" + str, true);
xmlhttp.send();
}
}
</script>
<body>
<input type = "text" onkeyup = "getStuff(this.value)"/>
<p><span id = "results"></span></p>
</body>
</html>