<form name="myform"> 姓名:<input type="text" id="name"></input> <input type="button" onClick="callRemote();" value="確定"></input> </form>
01 function callRemote() { 02 sUrl = "/jlu/servlet/HelloWorld?name=" + 03 encodeURIComponent(document.getElementById('name').value); 04 transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null); 05 }
<!-- Combo-handled YUI JS files: --> <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo/yahoo-min.js&2.7.0/build/event/event-min.js&2.7.0/build/connection/connection-min.js"></script>
var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
// 方法一(常用的方式) function helloWorld() { alert("Hello World"); } // 方法二(匿名方法) var helloWorld = function() { alert("Hello World"); };
var callback = { success: function(o) { // 回傳成功所要執行的程式碼 }, failure: function(o) { // 回傳失敗所要執行的程式碼 } };
var callback = { success: function(o) { alert(o.responseText); }, failure: function(o) { alert("錯誤:無法取得回應"); } };
<!-- Combo-handled YUI JS files: --> <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo/yahoo-min.js&2.7.0/build/event/event-min.js&2.7.0/build/connection/connection-min.js"></script> <script type="text/javascript"> var transaction; var sUrl; var callback = { success: function(o) { alert(o.responseText); }, failure: function(o) { alert("錯誤:無法取得回應"); } }; function callRemote() { sUrl = "/jlu/servlet/HelloWorld?name=" + encodeURIComponent(document.getElementById('name').value); transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null); } </script> </head>