jQuery and Pico Lisp
The heart of the problem with making jQuery.post work with the PicoLisp server is simple once found (thanks to Alex for helping me find it). Apache seems to use the Content-Length header to determine the length of the argument sent by XMLHttpRequest.send(), the Pico server doesn’t bother with determining the the length. It looks for a newline at the end instead.
How do we solve this problem considering that $.ajax does not append a newline? With my abysmal knowledge of OO programming in Javascript (all that prototyping makes my head hurt) I’ve opted for the simplest and dirtiest solution. A simple copy paste of $.post and $.ajax implemented as a separate plugin to enable effortless upgrades of the core, the new stuff can be called with $.pico.post and works just like the original.
The only change in the plugin compared with the original is line 2806 in jquery.js, it looks like this in the original: xhr.send(s.data);, I’ve changed that to xhr.send(s.data + ‘\r\n’);.
My current application renders the HTML like this:
(de js (JS)
(prinl "<script type=\"text/javascript\" src=\"" (pack *BP "js/" JS ) "\"></script>"))
(de rss-html Prg
(html 0 "RSS Reader" *Css NIL
(js "jquery.js")
(js "jquery.pico.js")
(js "rss-reader.js")
(<div> 'page_margins
(<div> 'page
(<div> 'header "header")
(<div> 'main
(<div> 'left
(<div> 'left_content (leftContent)))
(<div> 'middle
(<div> 'middle_content (run Prg 1)))
(<div> 'right
(<div> 'right_content "right"))
(<div> 'clear)))
(<div> 'footer "footer"))))
So we include the new plugin in the form of (js “jquery.pico.js”), after the base library.
The rss-reader.js file now contains this:
$(document).ready(function(){
$(".articles_link").css('cursor', 'pointer').click(function(){
$.pico.post("@ajaxTest", {jquerytest: "test"}, function(res){
$(".middle_content").html(res);
});
});
});
When one of the links are clicked we call @ajaxTest:
(de ajaxTest ()
(httpHead "text/plain; charset=utf-8")
(ht:Out T
(ht:Prin (pack "Result: " (get 'jquerytest 'http)))))
And the contents of the div with the class attribute of “middle_content” changes to Result: test, great.
Source: jquerypico.js
Note that we make use of the httpGate here, the base url in this example is http://localhost, not http://localhost:8080.