创建一个可重用的函数来打开和发送Ajax

S. *_*agu 5 javascript ajax

我已经注意到,每当我想声明一个对象时,在我的程序中,例如list,save,add,remove我每次在每个函数中都写下以下内容.

ajax.open("Get", "./route/action",true);     
ajax.send();
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情.

//this.ajax.get('./route/action').update('ajax-content');
./route/action // this is path to my Action class-using servlet
Run Code Online (Sandbox Code Playgroud)

每次我必须打开一个连接时,给我的动作类路径servlet,然后发送.我每次要加载列表,删除或更新时都这样做.

我怎么能写一个我将成为的函数:

this.ajax.get('./route/action');
// 'ajax.content' is the id of the div where I 
// want to show the list,where after updating AJAX show the update list to the user.
update('ajax-content'); 
Run Code Online (Sandbox Code Playgroud)

例如,在添加用户之后,我可以看到添加的用户而无需重新加载整个页面.我正在使用Java EE,Servlet和JavaScript开发maven项目.