jquery解析json数据

xulong1516519 2012-05-08 11:33:33
这是从数据库中返回数据组装的界面
json.jsp
<%@ page language="java" import="java.util.*,connection.*" pageEncoding="utf-8" contentType="text/xml"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//得到前台传过来的数据信息与后台数据库进行比对
String content= (String)request.getAttribute("content");
List<Map.Entry<String, Integer>> contentList= new Seach(content).result();
StringBuffer sBuffer= new StringBuffer();
//sBuffer.append("[{\"name\":\"aa\"}]");
Map<String, String> jsonMap= new HashMap<String, String>();
sBuffer.append("[{");
for(int i= 0; i< contentList.size(); i++) {
if(i< contentList.size()-1){
sBuffer.append("\"name\":\""+contentList.get(i).getKey()+"\",");
}else{
sBuffer.append("\"name\":\""+contentList.get(i).getKey()+"\"}]");
}
}

request.setAttribute("json",sBuffer.toString());
%>
这是调用的servlet;
AutoComplete.java
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
String content= request.getParameter("content");
request.setAttribute("content", content);
//使用json传递数据
request.getRequestDispatcher("json.jsp").forward(request, response);
String jsonContent= (String)request.getAttribute("json");
out.write(jsonContent.toString());
out.println(jsonContent.toString());
System.out.println(jsonContent);
out.flush();
out.close();
}
这是调用的js显示页面:
json.js

$post("AutoComplete", {content:wordText}, function(data){//成功的话进入回调函数
// var obj= "[{\"name\":\"data\",\"name\":\"ata\"}]";
var obj= data;
alert(obj.length);
var words= eval("("+obj+")");//解析每一个json元素
au.html("");
words.each(function(i) {//遍历每一个输入框并执行函数
var word= $(this);
alert(word.name);
var mouse= $("<div>").attr("id", i);
mouse.html(word.name).appendTo(au);
mouse.mouseover(function() {
if(highindex!= -1) {
au.children("div").eq(highindex)
.css("background-color", "white");
}
highindex= $(this).attr("id");
$(this).css("background-color", "gray")
});
mouse.mouseout(function() {
$(this).css("background-color", "white");

});
mouse.click(function() {//eq()获得指定位置上的jq对象
var inputContent= au.children("div").eq(highindex).text();
var inputContent= $(this).text();
highindex= -1;
au.hide();
$("#word").val(inputContent);
$("#word").focus();
});
});
if(words.length> 0) {
au.show();
}else{
au.hide();
highindex= -1;
}
},"json")

我在js上就是得不到servlet回传回来的data数据。求解
...全文
313 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
001007009 2012-05-08
  • 打赏
  • 举报
回复
后台打印的内容是什么
json格式正确吗?
xulong1516519 2012-05-08
  • 打赏
  • 举报
回复
是success的,我用xml数据格式时能传数据的,而且成功显示,但换成json就不行了,这还是原来的解析xml数据格式是的js文件,只是改了数据解析的格式"xml"->"json"
001007009 2012-05-08
  • 打赏
  • 举报
回复
单独运行下 jsp 能跑起来吗
ajax 请求成功吗?
xulong1516519 2012-05-08
  • 打赏
  • 举报
回复
我刚开始是想测试跳转的,所以就直接用jsp处理了,那你说怎样从json。jsp输出到前台的js中啊。还请赐教
  • 打赏
  • 举报
回复
为什么要SERVLET转JSP,他们两个哪一个都可以处理啊,你这太乱了,真的!还有就是json.jsp没有输出东西到前台啊,前台怎么接收数据?
xulong1516519 2012-05-08
  • 打赏
  • 举报
回复
[{"name":"a","name":"aaab","name":"abcdef","name":"ab","name":"abc"}]
这是我打印出来的json格式。应该正确吧
xulong1516519 2012-05-08
  • 打赏
  • 举报
回复
[{"name":"a","name":"aaab","name":"abcdef","name":"ab","name":"abc"}]
这是我打印出来的json格式。应该正确吧

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧