Roger Meier | f04cfca | 2010-10-15 19:24:32 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 2 | <!-- |
| 3 | Licensed to the Apache Software Foundation (ASF) under one |
| 4 | or more contributor license agreements. See the NOTICE file |
| 5 | distributed with this work for additional information |
| 6 | regarding copyright ownership. The ASF licenses this file |
| 7 | to you under the Apache License, Version 2.0 (the |
| 8 | "License"); you may not use this file except in compliance |
| 9 | with the License. You may obtain a copy of the License at |
| 10 | |
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | Unless required by applicable law or agreed to in writing, |
| 14 | software distributed under the License is distributed on an |
| 15 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | KIND, either express or implied. See the License for the |
| 17 | specific language governing permissions and limitations |
| 18 | under the License. |
| 19 | --> |
| 20 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 21 | <head> |
| 22 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 23 | <title>Thrift Javascript Bindings - Tutorial Example</title> |
| 24 | |
Roger Meier | ea4600b | 2013-09-21 08:47:21 +0200 | [diff] [blame] | 25 | <script src="../../lib/js/src/thrift.js" type="text/javascript"></script> |
Roger Meier | fa89932 | 2012-10-22 19:27:38 +0000 | [diff] [blame] | 26 | <script src="gen-js/tutorial_types.js" type="text/javascript"></script> |
| 27 | <script src="gen-js/shared_types.js" type="text/javascript"></script> |
| 28 | <script src="gen-js/SharedService.js" type="text/javascript"></script> |
| 29 | <script src="gen-js/Calculator.js" type="text/javascript"></script> |
Roger Meier | f04cfca | 2010-10-15 19:24:32 +0000 | [diff] [blame] | 30 | |
| 31 | <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> |
| 32 | |
| 33 | |
| 34 | <script type="text/javascript" charset="utf-8"> |
| 35 | //<![CDATA[ |
| 36 | $(document).ready(function(){ |
| 37 | // remove pseudo child required for valid xhtml strict |
| 38 | $("#op").children().remove(); |
| 39 | // add operations to it's dropdown menu |
| 40 | $.each(Operation, function(key, value) { |
| 41 | $('#op').append($("<option></option>").attr("value",value).text(key)); |
| 42 | }); |
| 43 | |
| 44 | $('table.calculator').attr('width', 500); |
| 45 | }); |
| 46 | |
| 47 | function calc() { |
| 48 | var transport = new Thrift.Transport("/thrift/service/tutorial/"); |
| 49 | var protocol = new Thrift.Protocol(transport); |
| 50 | var client = new CalculatorClient(protocol); |
| 51 | |
Roger Meier | 443ced0 | 2014-03-26 23:23:37 +0100 | [diff] [blame] | 52 | var work = new Work(); |
Roger Meier | f04cfca | 2010-10-15 19:24:32 +0000 | [diff] [blame] | 53 | work.num1 = $("#num1").val(); |
| 54 | work.num2 = $("#num2").val(); |
| 55 | work.op = $("#op").val(); |
| 56 | |
| 57 | try { |
| 58 | result = client.calculate(1, work); |
| 59 | $('#result').val(result); |
| 60 | $('#result').css('color', 'black'); |
| 61 | } catch(ouch){ |
| 62 | $('#result').val(ouch.why); |
| 63 | $('#result').css('color', 'red'); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | function auto_calc() { |
| 68 | if ($('#autoupdate:checked').val() !== undefined) { |
| 69 | calc(); |
| 70 | } |
| 71 | } |
| 72 | //]]> |
| 73 | </script> |
| 74 | |
| 75 | </head> |
| 76 | <body> |
| 77 | <h2>Thrift Javascript Bindings</h2> |
| 78 | <form action=""> |
| 79 | <table class="calculator"> |
| 80 | <tr> |
| 81 | <td>num1</td> |
| 82 | <td><input type="text" id="num1" value="20" onkeyup="javascript:auto_calc();"/></td> |
| 83 | </tr> |
| 84 | <tr> |
| 85 | <td>Operation</td> |
| 86 | <td><select id="op" size="1" onchange="javascript:auto_calc();"><option></option></select></td> |
| 87 | </tr> |
| 88 | <tr> |
| 89 | <td>num2</td> |
| 90 | <td><input type="text" id="num2" value="5" onkeyup="javascript:auto_calc();"/></td></tr> |
| 91 | <tr> |
| 92 | <td>result</td> |
| 93 | <td><input type="text" id="result" value=""/></td></tr> |
| 94 | <tr> |
| 95 | <td><input type="checkbox" id="autoupdate" checked="checked"/>autoupdate</td> |
| 96 | <td><input type="button" id="calculate" value="calculate" onclick="javascript:calc();"/></td> |
| 97 | </tr> |
| 98 | </table> |
| 99 | </form> |
| 100 | |
Robert Lu | a139082 | 2018-12-27 23:57:35 +0800 | [diff] [blame] | 101 | <p>This Java Script example uses <a href="https://github.com/apache/thrift/blob/master/tutorial/tutorial.thrift">tutorial.thrift</a> and a Thrift server using JSON protocol and HTTP transport. |
Roger Meier | f04cfca | 2010-10-15 19:24:32 +0000 | [diff] [blame] | 102 | </p> |
| 103 | <p> |
| 104 | <a href="http://validator.w3.org/check/referer"><img |
| 105 | src="http://www.w3.org/Icons/valid-xhtml10" |
| 106 | alt="Valid XHTML 1.0!" height="31" width="88" /></a> |
| 107 | </p> |
| 108 | </body> |
| 109 | </html> |