I'm trying to formulate code, which would allow me to print something inside an html input field, and then the Haskell code will return what I have printed in the next div (to make an Ajax search engine in the long run). The connection between the two seems established, but in a really strange way. More than that, I do not quite understand how to make cases for the queryString req in the following sequence. Here is the Haskell code:
{-# 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"
And here is my search page:
<!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>