The following introduction shows how the Barracuda server is used in designing AJAX and RESTful web-services.
The AJAX example below
runs on our online server.
In the following example, a basic calculator is designed as a standard web application, and we will later enhance this calculator by using Ajax. To keep it simple, the calculator will be designed to add two numbers only.
1 <?lsp 2 3 local var1 4 local var2 5 local result = "" 6 7 -- Fetch all URL encoded (POST'ed form) data sent from client as a Lua table. 8 local d = request:data() 9 if d.var1 and d.var2 then -- If client sends data 10 -- Convert URL encoded data to numbers 11 var1 = tonumber(d.var1) 12 var2 = tonumber(d.var2) 13 -- var1 and/or var2 are nil if data could not be converted to a number. 14 -- Set result = var1+var2 if var1 and var2 are OK, otherwise set result to 15 -- "???" 16 result = var1 and var2 and (var1+var2) or "???" 17 end 18 -- Else, a standard browser GET 19 20 ?> 21 <html> 22 <head> 23 <title>Addition</title> 24 </head> 25 <body> 26 <form method="post"> 27 <input type="text" name="var1" value="<?lsp=var1 and var1 or ""?>"/> 28 + 29 <input type="text" name="var2" value="<?lsp=var2 and var2 or ""?>"/> 30 = <span id="result"><?lsp=result?></span> 31 <br/> 32 <input type="submit" value="Submit"/> 33 </form> 34 </body> 35 </html>
The html form starts on line 26 and ends on line 33. The user enters a number into the two text fields (line 27 and 29). The data is sent as URL encoded data to the server when the user presses the submit button on line 32. The URL-encoded data for the above form is sent as: var1=10&var2=11, where 10 and 11 are numbers entered into the text fields.
Lua script code starting on line 1 and ending on line 20 is executed by the server every time the browser sends a request to the LSP page. If the browser sends the two URL-encoded variables var1 and var2, line 9 to 17 in the above code is executed on the server. The server side code extracts the variables and converts the variables to numbers.
The "result" is dynamically inserted into the generated HTML on line 30. We also update the value attributes on line 27 and 29. Updating the value fields make the input variables persistent -- i.e. the HTML code returned in response to a "browser POST request" have the text fields set to the previous value.
It is common to send Ajax data from the browser to the client as URL-encoded data. The server side code for extracting the URL-encoded data and calculating the result (line 12-17) can therefore be used as it was in the original page. The new Lua code starting on line 20 checks if the request is from an Ajax call. If it is, the result is assembled into a Lua table, converted to JSON, and sent to the browser.
The code starting on line 68 uses the JQuery ready event to automatically execute the code at startup when the page is loaded by the browser.
RESTful web-services can easily be designed using the directory type. The getting started guide includes an example of how one can create RESTful web-services and use logical URL's.