ajax 如何返回一个对象,如何返回一个JSON对象,并赶上在HTML Ajax调用(How to return an json...

我在我的D3代码的信息的简单树结构。 D3的脚本呼吁从脚本d3.json()函数JSON和所产生的JSON是给树结构data.Now我有一对夫妇的信息将来自数据库,并击中了Servlet。 我必须使JSON动态地在Servlet,以便它可以在用户改变response.Here是脚本&我用JSON ..

这是从那里我做了一个Ajax调用Servlet的我的HTML文件。 阿贾克斯是调用Servlet中,但在取回响应错误。 我收到“发生错误”的message.When我运行servlet ......

function doajax(){

$.ajax({

url: "AccountServlet",

type: "post",

dataType: "json",

error:function(){

alert("error occured!!!");

},

success:function(data){

alert(data.fullName + "\n" + data.mobileNo);

}

});

}

这是我在哪里特林找回这个文件只是调用我得到一个错误occuree消息的响应Servlet.But响应中的HTML ...

IN的accountServlet我创造这样的JSON

ArrayList countList = new ArrayList();

countList.add(new DistinctSourceCount("Jan", 1800));

countList.add(new DistinctSourceCount("Feb", 1500));

countList.add(new DistinctSourceCount("March", 2000));

countList.add(new DistinctSourceCount("April", 1550));

countList.add(new DistinctSourceCount("May", 1000));

countList.add(new DistinctSourceCount("June", 1700));

countList.add(new DistinctSourceCount("July", 1400));

countList.add(new DistinctSourceCount("Aug", 1900));

countList.add(new DistinctSourceCount("Sept", 1000));

countList.add(new DistinctSourceCount("Oct", 1500));

countList.add(new DistinctSourceCount("Nov", 1100));

countList.add(new DistinctSourceCount("Dec", 2000));

Gson gson = new Gson();

JsonArray arrayObj = new JsonArray();

for (int i = 0; i < countList.size(); i++) {

DistinctSourceCount count = countList.get(i);

JsonElement linObj = gson.toJsonTree(count);

arrayObj.add(linObj);

}

JsonObject myObj = new JsonObject();

myObj.addProperty("success", true);

myObj.add("topList", arrayObj);

myObj.addProperty("totalCount", countList.size());

System.out.println(myObj.toString());

System.out.close();

这是我的代码,我已经这样做了far.Can谁能帮助我了解如何创建Servlet的JSON对象并获取回响应脚本?请任何人的帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值