public class SimpleCallback
{
public String hello(String name)
{
return name+"简单回调与json回调";
}
}
SimpleCallBack对应的javascript对象为hello
简单回调:
//data为服务器响应数据 function callbac(data) { alert(data); } //javascript调用 hello.hello("wang",callbac);
也可使用匿名回调:
hello.hello("wang",function(data) { alert(data); });
json回调:
hello.hello("wang", { callback:cb, timeout:5000, errorHandler:function(message){alert("错误提示:"+message);}, warningHandler:function(message){alert("Oops:"+message);}, textHtmlHandler:function(message){alert("Oops:"+message);}, exceptionHandler:function(message){alert("Oops:"+message);}, httpMethod:'POST', async:true, rpcType:dwr.engine.XMLHttpRequest, preHook:function(){alert('远程调用之前');}, postHook:function(){alert('远程调用之后');} }); function cb(data) { alert(data); }
回调函数只有一个参数,如果需要将客户端的其他javascript变量也传入回调函数,应使用适配器模式:
var fromBrowser = "客户端变量"; function sendMessage() { hello.hello("wang", { callback:cb, timeout:5000, errorHandler:function(message){alert("错误提示:"+message);}, warningHandler:function(message){alert("Oops:"+message);}, textHtmlHandler:function(message){alert("Oops:"+message);}, exceptionHandler:function(message){alert("Oops:"+message);}, httpMethod:'POST', async:true, rpcType:dwr.engine.XMLHttpRequest, preHook:function(){alert('远程调用之前');}, postHook:function(){alert('远程调用之后');} }); } function cb(data) { db(data , fromBrowser); } function db(data , fromBrowser) { document.getElementById("show").innerHTML=data; alert(fromBrowser); }