Are there any good examples or tutorials demonstrating AJAX or AJAJ with SWI-Prolog?

132 views
Skip to first unread message

Shon Feder

unread,
May 8, 2014, 2:31:49 AM5/8/14
to swi-p...@googlegroups.com
Hello, All,

I have spent some time studying the various http/... libraries, and have gotten as far as serving and generating dynamic pages, serving static content. However, I have had a very hard time figuring out how to realize a web app using AJAX or AJAJ. It doesn't help that this is my first foray into such technologies, but I also haven't been able to track down any clear examples or tutorials on how to do this. Ann Ogborn's tutorial is helpful, but doesn't discuss the topic at all as far as I know, and the topic has been on the todo list of the SWI "How to collection" for a long while.

I would be grateful for any pointers to help me get oriented here. I would prefer to figure this out using Prolog, since it is the language I know best and I have areal affection for it; however,  I would be receptive to suggestions to begin learning AJAX with some more common language, should you all think it's necessary.

Lastly, should I be looking at `library(pengines)` for doing this sort thing?

Many thanks in advance,

Shon

Michael Ben Yosef

unread,
May 9, 2014, 4:08:43 AM5/9/14
to swi-p...@googlegroups.com
Hi Shon,


I have spent some time studying the various http/... libraries, and have gotten as far as serving and generating dynamic pages, serving static content. However, I have had a very hard time figuring out how to realize a web app using AJAX or AJAJ. It doesn't help that this is my first foray into such technologies, but I also haven't been able to track down any clear examples or tutorials on how to do this. Ann Ogborn's tutorial is helpful, but doesn't discuss the topic at all as far as I know, and the topic has been on the todo list of the SWI "How to collection" for a long while.

I don't know of any tutorials, but if you want an example, you can take a look at how the SWI-Prolog website handles autocompletion in the search bar using AJAJ. You may have to jump between files to get the full picture, but most of the relevant code is at the top of autocomplete.pl. The multifile DCG rule prolog:doc_search_field//1 is used to generate the HTML for the search bar. ac_predicate/1, defined just below, is the actual handler for the AJAJ requests. prolog:doc_search_field//1 calls http_link_to_id(ac_predicate, [], URL), to get the URL (URL) for this handler, then uses the quasiquotation syntax to generate JavaScript with jQuery that causes the relevant events on the search bar to send an AJAX (actually, AJAJ) request to this URL.

Now look at ac_predicate/1. It does the work to find the completions, then build up a JSON term containing them and then simply calls reply_json/1 on it. The latter does all the work of turning the term into a JSON string and sending it as a reply to the request.
 
I would be grateful for any pointers to help me get oriented here. I would prefer to figure this out using Prolog, since it is the language I know best and I have areal affection for it; however,  I would be receptive to suggestions to begin learning AJAX with some more common language, should you all think it's necessary.

I'm in a similar boat to you and I don't think its necessary to learn this with another language first. After all, an AJAX (or AJAJ) request is just a normal HTTP request involving XML (or JSON), and SWI-Prolog already has all the facilities to make these easy on the server-side. And what you have to do in the JS on the client-side doesn't depend on the web framework you're using on the server, anyway.
 
Lastly, should I be looking at `library(pengines)` for doing this sort thing?

I don't think so.
 
Hope that helps!

Michael
(You know me as "mycl" on reddit, by the way.)

Shon Feder

unread,
May 11, 2014, 1:28:03 AM5/11/14
to swi-p...@googlegroups.com
Michael,

That's a great help! I'm grateful for the pointer to the autocomplete.pl and for the more specific guidance on how to parse and understand the code.  I'm looking forward to diving in this week and, hopefully, finally wrapping up my project!

(I'm glad to encounter you outside of the reddit milieu! I've quit reddit for the time being, and the only thing I miss--from time to time--is the intermittent discussions on r/prolog. Many of your remarks there have had a lasting influence on the way I understand and study the language. Thanks for that.)

Best wishes,
Shon
Reply all
Reply to author
Forward
0 new messages