jQuery - ajax

Posted 你的笑忘书

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jQuery - ajax相关的知识,希望对你有一定的参考价值。

1. Java 代码

public class HandlerAction {
	private List<T> list;// 或 ArrayList<HashMap<Object, Object>> list
	private String jsonStr;
	// getter && setter
	
	 public String doSth() throws Exception {
		 list = ...;// 直接返回 list,在 ajax 的 success 下面使用
		 
		 // 或者是转换成 json 字符串,然后再在 ajax 里转换成 json 对象后使用
		 jsonStr = new net.sf.json.JSONArray().fromObject(list).toString();// 1
		 jsonStr = new net.sf.json.JSONObject().fromObject(list).toString();// 2
		 
		 return "success";
	 }
}

  

2. 关于 $(function() {});

 

3. Ajax

<script type="text/javascript">
	$(function() {
		$.ajax({
			url: "",
			data: {
				"arg0": arg0,
				"arg1": arg1
			},
			async: true,
			dataType: "json",
			success: function(data) {
				var html = ‘‘;
				// 遍历方式 1
				// jsonArray 是后台返回的 json 字符串
				$.each(eval(‘(‘ + data.jsonStr + ‘)‘),
				function(i, item) {
					html += ‘<li class="green"><h3>‘ + item.time + ‘</h3><dl><dt><span>‘ + item.content + ‘</span></dt></dl></li>‘;
				});
				$("#timeline").append(html);

				// 遍历方式 2
				// list 是后台返回的 List<T> list
				/* for ( var i in data["list"]) {
					html += ‘<li class="green"><h3>‘ + data["list"][i].time + ‘</h3><dl><dt><span>‘ + data["list"][i].content + ‘</span></dt></dl></li>‘;
				}
				$("#timeline").append(html); */

			},
			error: function() {
				alert("系统错误");
			}
		});
	});
</script>

 

4. $.each(obj, function(i, item) {});

以上是关于jQuery - ajax的主要内容,如果未能解决你的问题,请参考以下文章

jQuery高级Ajax

十条jQuery代码片段助力Web开发效率提升

十条jQuery代码片段助力Web开发效率提升

使用 JQuery ajax 在 DOM 操作后附加事件

使用 Jquery 的同步“Ajax”调用似乎不起作用

前端面试题之手写promise