前台jquery+ajax向后台请求数据,后台返回json,前台显示
16lz
2021-01-22
背景:jsp+struts2
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript" src="../js/jquery-1.7.min.js">
</script>
<script type="text/javascript">
$().ready(function() {
$('#selectNum').change(function() {
$.post('TestJQAJAX',
function(data) {
var pros = eval(data);
// $("#divTest").append("<tr><td>"+姓名+"</td></tr><tr><td>"+年龄+"</td></tr>");
$("#tabTest").empty();
for (var i = 0; i < pros.length; i++) {
var cityName = pros[i].name;
var cityValue = pros[i].age;
$("#tabTest").append("<tr><td>" + cityName + "</td><td>" + cityValue + "</td></tr>");
}
});
})
})
</script>
</head>
<body>
<select id="selectNum">
<option value="0">
--Select--
</option>
<option value="1">
1
</option>
<option value="2">
2
</option>
<option value="3">
3
</option>
</select>
<div id="divTest">
<table id="tabTest" border="1" cellspacing="0" bgcolor="green"></table>
</div>
</body>
</html>
action页面
package com.xiaoma.TestAction;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xiaoma.po.User;
public class TestJQ extends ActionSupport{
public void myExecute() throws IOException{
ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
PrintWriter out = ServletActionContext.getResponse().getWriter();
// String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"张三\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}";
//String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"张三\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}";
// String jsonString="{'success':'true','msg':'用户名或者密码错误!'}";
// String jsonString="{'success':'true','msg':'用户名或者密码错误!'}";
String jsonString="[{\"name\":\"malei\",\"age\":1,\"position\":\"henan\"}, {\"name\":\"malei2\",\"age\":3,\"position\":\"henan2\"}]";
System.out.println(jsonString);
out.println(jsonString);
out.flush();
out.close();
}
}
更多相关文章
- 点击JSON数据加载Galleria画廊。我需要新鲜的眼睛来看我的错误
- jQuery:离线后发布错误(iOS和Chrome)
- 在线请教调用Jquery错误:TypeError: a is undefined 的错误原因
- 在发出xml Ajax请求时获取错误412
- Internet Explorer导致无效的真实性令牌错误
- DataTables警告:table id = DataTables_Table_0 - Ajax错误。有
- 如何在使用jquery验证和自定义错误放置时清除错误
- Ajax调用php脚本返回404错误
- Jquery验证插件,获取错误字符串