I am on Linux -both browser side & server side- with a recent Firefox 38 or 42 if that matters; this question gives more context, and the github GPLv3 project containing my code. It is not a usual Web application (it would have usually one, and perhaps a dozen, of simultaneous Web users). I am writing or generating both server & browser side code
Let's suppose I have some HTML5 code like
<div id="mydyndiv_id"></div>
I am making an AJAX request with JQuery. On success it should insert some (AJAX generated) HTML element, e.g. <b>bold</b>
(in reality it is a much bigger HTML fragment with nested <span>
-s whose content is dynamically generated from the POST argument of the AJAX request), into that div
and call some other Javascript function doit
, e.g. doit(42)
only once just after the AJAX request (e.g. that function would clear some other <textarea>
in my page, and the 42
argument is provided by the AJAX response). I can change code both on server side (e.g. alter the AJAX processing) and on browser side.
What is the most idiomatic way to achieve that?
making a JSON AJAX which contains both the inserted HTML & the function argument, so the AJAX response could be
{"text":"<b>bold</b>", "data": 42}"
ofContent-type: "application/json"
and the Javascript code would be$.ajax ({url: "/someajax", method: "POST", data: {"somearg": "foo"}, datatype: "json", success: function(jsa) { $("#mydyndiv_id").html(jsa.text); doit(jsa.data); }});
this is rather heavy, the server should double-encode HTML&JSON the HTML fragment: it needs first to construct the
<b>bold</b>
fragment -with HTML encoding, and then to construct the JSON object and send it.making an HTML AJAX which has some
<script>
element. The AJAX response would be ofContent-type: text/html
and would contain<b>bold</b><script>doit(42)</script>
, and the Javascript code would be$.ajax ({url: "/someajax", method: "POST", data: {"somearg": "foo"}, datatype: "html", success: function(ht) { $("#mydyndiv_id").html(ht); }});
this might be wrong, since the
doit(42)
function could be perhaps called more than once and is kept in the DOM and I don't want thatmaking a Javascript AJAX; the AJAX response would be of
Content-type: application-javascript
and would contain:$("#mydyndiv_id").html("<b>bold</b>"); doit(42);
with the AJAX invocation in Javascript being
$.ajax ({url: "/someajax", method: "POST", data: {"somearg": "foo"}, datatype: "script", success: function(jscode) { /* empty body */ } })
This is brittle w.r.t. errors in
doit(42)
(see this question; the only debugging technique I found is lots ofconsole.log
and that is painful) and also requires double encoding on server side.
Of course, any other technique is welcome!
PS. If you are curious, the code is commit a6f1dd7514e5 of the MELT monitor (alpha stage) and you would try the http://localhost.localdomain:8086/nanoedit.html
URL in your browser; this software (which is also a specialized HTTP server!) would have only very few simultaneous Web users (usually one, perhaps a dozen); in that sense it is not a usual web application. In my dreams it could become a workbench for a small team of (C & C++) software developers, and the GUI of that workbench would be their browser.