ajax如何实现页面局部跳转与结果返回

通过代码示例分析给大家介绍ajax实现页面局部跳转与结果返回,具体内容如下:

1、带有结果返回的提交过程

这里用一个提交按钮来演示,HTML代码为:

<input type="button" value="提报">

点击提报按钮后,通过ajax来实现跳转到action中处理,JavaScript代码为:

function tibao(){ var; var URL = <select:link page="/smokeplan.do?method=Tibao&idset="/>+id; $.ajax({url: URL,   type: 'GET',   success: function(result) { alert(result); } }); }

action处理完成后,将返回的结果放到result中,在页面弹出提示信息;当然这里的action跳转是需要配置xml的。

后台Java类处理过程为:

//提报 public void Tibao(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String idset=request.getParameter("idset"); CallHelper helper = initializeCallHelper("L_SmokeBoxtibaoWLDan", form,request, false); helper.setParam("bill_ids",idset); helper.setParam("personid",getPersonId(request)); helper.execute(); PrintWriter write = response.getWriter(); write.print(helper.getOutput("message")); write.close(); }

这里是通过一个sql语句对数据进行处理,返回一个message,并将信息打印到页面;

这里做的操作的结果是反映到response对应的位置,于是拿到属于response的流,而不是new一个出来。

也就是说我从那里跳转过来的,我这个信息就会返回到那里去。所以在js中就可以用result进行接收这个返回结果,并且用alert提示。

使用AJAX如何实现页面跳转

示例代码如下:

项目当中采用了ajaxAnywhere框架来实现ajax,效果不错,并且容易实现,但现在问题是即使页面实现了效果,业务上还需要提交表单,在这种情况下,即使点击提交后,它仍然会刷新你定义好的zone区域,这个时候,如果单纯的提交表单就不够了,我采取的方案是:

利用js这个强大的BS项目开发工具,自定义一个函数来解决上述问题:

function doGuahao() { if(checkdata()) { if(document.form1.result_flag.value=="0") { return false; } else { if(document.form1.checktype.value=="danganhao") { form1.action = "<%=formAction%>"; form1.submit(); } if(document.form1.checktype.value=="xingming") { form1.action = parent.left.url2; form1.submit(); } if(document.form1.checktype.value=="shenfenzheng") { form1.action = "<%=formAction%>"; form1.submit(); } } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wggxyg.html