title: Lift session-valid ajax callback from a static javascript
body: <p>I am currently implementing a graph visualisation tool using lift on the server side and d3 ( a javascript visualisation framework) for all the visualisation. The problem I have is that in the script I want to get session dependent data from the server. </p>
  
  <p>So basically, my objective is to write lift-valid ajax callbacks in a static js script. </p>
  
  <h1>What I have tried so far</h1>
  
  <p>If you feel that the best solution is one that I already tried feel free to post a detailed answer telling me how to use it exactly and how it completely solves my problem. </p>
  
  <h3>Write the ajax callback in another script using lift and call it from the main script</h3>
  
  <p>This solution, which is similar to a hidden text input is probably the more likely to work. However it is not elegant and it would mean that I would have to load a lot of scripts on load, which is not really conveniant. </p>
  
  <p>This seems to be one of the prefered solutions in the lift community as explained in <a href="https://groups.google.com/forum/#!topic/liftweb/ZDSsDMj-KRk/discussion" rel="nofollow noreferrer">this discussion on the mailing list.</a></p>
  
  <h3>REST interface</h3>
  
  <p>Usually what one would do to get data from a javascript function in lift is to create a REST interface. However this interface will not be linked to any session. This is the solution I got from my previous question: <a href="https://stackoverflow.com/questions/12800612/get-json-data-in-d3-from-lift-snippet">Get json data in d3 from lift snippet</a></p>
  
  <h3>Give function as argument of script</h3>
  
  <p>Another solution would be to give the ajaxcallback as an argument of the main script called to generate my graph. However I expect to have a lot of callbacks and I don't want to have to mess with the arguments of my script.</p>
  
  <h3>Write the whole script in lift and then serve it to the client</h3>
  
  <p>This solution can be elegant, however my script is very long and I would really prefer that it remainss static.  </p>
  
  <h1>What I want</h1> 
  
  <h3>On client side</h3>
  
  <p>While reviewing the source code of my webpage I found that the callback for an ajaxSelect is: </p>
  
  <pre><code>&lt;select onchange="liftAjax.lift_ajaxHandler('F966066257023LYKF4=' + encodeURIComponent(this.value), null, null, null)" name="F96606625703QXTSWU" id="node_delete" class="input"&gt;
  </code></pre>
  
  <p>Moreover, there is a variable containing the state of the page in the end of the webpage: </p>
  
  <pre><code>var lift_page = "F96606625700QRXLDO";
  </code></pre>
  
  <p>So, I am wondering if it is possible to simulate that my ajaxcall is valid using this <code>liftAjax.lift_ajaxHandler</code> function. However I don't know the exact synthax to use. </p>
  
  <h3>On server side</h3>
  
  <p>Since I "forged" a request on client side, I would now like to get the request on client side and to dispatch it to the correct function. This is where the <code>LiftRules.dispatch</code> object seems the best solution: when it is called, all the session management has been made (the request is authentified and linked to a session), however I don't know how to write the correct piece of code in the <code>append</code> function. </p>
  
  <h3>Remark</h3>
  
  <p>In lift all names of variables are changed to a random string in order to increase the security, I would like to have the same behavior in my application even if that will probably mean that I will have to "give" the javascript these values. However an array of 15 string values is still a better tradeoff than 15 functions as argument of a javascript function. </p>
  
  <h3>Edit</h3> 
  
  <p>While following my research I found this page : <a href="https://www.assembla.com/spaces/liftweb/wiki/Mapping_server_functions_to_client_actions" rel="nofollow noreferrer">Mapping server functions to client actions</a> which somehow explains the goal of named functions even if it stil didn't lead me to a working solution. </p>