ajax动态数据解析,ajax动态加载json数据并详细解析

效果图

64b9d5d4bf65d287eda591ff6cda23e6.png

姓名:

年龄:

servlet代码

//get

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

response.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=utf-8");

String name = request.getParameter("name");

String age = request.getParameter("age");

if (name == null || name == "") {

name = "测试名字admin";

}

if (age == null || age == "") {

age = "测试年龄100";

}

user user = new user(1, name, age);

PrintWriter out = response.getWriter();

JSONObject jsonObj = JSONObject.fromObject(user);

out.print(jsonObj);

out.flush();

out.close();

}

//post

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// response.setContentType("text/html");

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=utf-8");

String name = request.getParameter("name");

if (name == null || name == "") {

name = "测试名字admin";

}

String age = request.getParameter("age");

if (age == null || age == "") {

age = "测试年龄100";

}

user user = new user(1, name, age);

PrintWriter out = response.getWriter();

JSONObject jsonObj = JSONObject.fromObject(user);

out.print(jsonObj);

out.flush();

out.close();

}

JS核心代码

//get

$(document).ready(function() {

$('form .get').click(function() {

$.get('ajaxServlet',function(response,status,xhr){

var dataObj = eval("(" + response + ")");

$("#box").html(response);

alert(dataObj.name);

});

});

//post

$('form .post').click(function() {

$.post('ajaxServlet',function(response,status,xhr){

var dataObj = eval("(" + response + ")");

$("#box").html(response);

});

});

//ajax

$('form .ajax').click(function() {

alert($("[name='name']").val());

$.ajax({

type:'get',

url:'ajaxServlet',

data:{

name:$("[name='name']").val(),

age:$("[name='age']").val()

},

success:function(response, status, xhr){

$("#box").html(response);}

});

});

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值