当前位置: 代码迷 >> Java Web开发 >> 求教ajax怎么不刷新传值
  详细解决方案

求教ajax怎么不刷新传值

热度:40   发布时间:2016-04-16 22:14:32.0
求教ajax如何不刷新传值
本帖最后由 showbo 于 2014-06-30 17:55:52 编辑
今天第一次接触ajax
在页面把要处理的sql传给Action:
$.ajax({
type: "POST",
url: URL+'!dealSql.action',
data: {Sql : Sql},
cache: false,
dataType: "json",
timeout: REQUEST_TIME,
error: function (XMLHttpRequest, textStatus, errorThrown) {
                          ////
},
beforeSend: function(XMLHttpRequest){
//////
}

});


在Action 中进行处理,setAttribute将结果再传回来(同一页面):
public String dealSql(){
HttpServletRequest request = Struts2Utils.getRequest();
HttpServletResponse response = Struts2Utils.getResponse();
String templateSql = getQueryParameter(request, "Sql");
List<String> names=new ArrayList<String>();
Pattern p = Pattern.compile("(##\\w*##)");
Matcher m = p.matcher(templateSql);
while(m.find()){
names.add(m.group());
}
request.setAttribute("names", names);
return "build";
}

然后jsp中:${names } 得不到想要的数据.......
------解决方案--------------------
使用AJAX的时候你的request还是之前的请求的
所以为了获取数据要在回调函数中使用js操作
------解决方案--------------------
参考  json

------解决方案--------------------
你都跳转了,当前页肯定获取不到names了,让这个方法返回值为空,在回调函数里就不能遍历动态生成页面了?
  相关解决方案