-
Notifications
You must be signed in to change notification settings - Fork 1
AJAX interface
dmi edited this page Sep 13, 2010
·
36 revisions
{module: "M", action: "A", event: "E", data: D}
- event is optional, when omited, E = “M:A” is assumed
- data is optional
- D is any JSON value
{event: "E-atom", reply: R}
- atom is “ok” | “fail”, but can also be other word
- R is any JSON value
<script src="js/json2.js" type="text/javascript"></script> <script src="MochiKit/MochiKit.js" type="text/javascript"></script> <script src="js/enge.js" type="text/javascript"></script> <script type="text/javascript"> function pageLoadFunction(){ // on-click handlers connect('testButton', 'onclick', doTest); // ajax reply event handlers connect(xapi, 'test:test-ok', cbTestOk); connect(xapi, 'test:test-fail', cbTestFail); // optional handler } MochiKit.Signal.connect(window, "onload", pageLoadFunction); </script>
function doTest(){ var json = { "module": "test", "action": "test", } ajax(json, "/enge"); }
function cbTestOk(json){ // json.reply is object $('testResult').innerHTML = json.reply.preamble + json.reply.sid; Highlight('testResult'); } function cbTestFail(json){ // json.reply is plain value $('testResult').innerHTML = "Error: " + json.reply; Highlight('testResult'); }
Erlang module must be prefixed with “ajax”, which is omited in JSON request.
Any function, exported from ajax_Something module can be accessed via JSON request (caller’s permissions are checked).
Function result is: {atom(), value(), headers()}
- atom(): ok | fail | something-other
- value(): erlang representation of JSON value
- headers(): list of headers to set via http (in future versions may contain other directives to web engine)
-module(ajax_test). -compile(export_all). -include("session.hrl"). test(Struct, Session, _Req) -> case Session of #session{sid = {auth, Sid}} -> % reply is object {{ok, {obj, [{"preamble", <<"Test ok the sid is">>}, {"sid", list_to_binary(Sid)}]}}, []}; _ -> % reply is binary {{fail, <<"No session">>}, []} end.