ajax如何接收返回的json数据

wula0010 2009-11-03 11:00:52
服务器端:
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.write(retJSONString);


前台页面:
xmlHttp.open("POST", BeanName);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(str);

//ajax返回处理
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
var jsonObj=eval(xmlHttp.responseText); //这样不行.......
.....
}
...全文
1093 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wula0010 2009-11-20
  • 打赏
  • 举报
回复
............
wula0010 2009-11-03
  • 打赏
  • 举报
回复
楼上的:
你直接alert(xmlHttp.responseText);
什么都不显示,一个空的alert窗口。
服务器和提交都没问题我用jquery的$.getJSON(BeanName,str,callFn);可以得到jsonObj,没有问题。
fengsky491 2009-11-03
  • 打赏
  • 举报
回复
你直接alert(xmlHttp.responseText);
看看是什么玩意先

在一步一步挑错
wula0010 2009-11-03
  • 打赏
  • 举报
回复
引用 2 楼 fireelf000 的回复:
要写成这样eval( "(" + xmlHttp.responseText + ")" )

这个报语法错误,请注意服务器端是:
response.setContentType("application/json;charset=UTF-8");

不用response.setContentType("application/x-javascript;charset=UTF-8");
是据说数据格式不严谨会有错误,请大家继续.......
fengsky491 2009-11-03
  • 打赏
  • 举报
回复
response.setContentType("application/json;charset=UTF-8");
改成
response.setContentType("application/x-javascript;charset=UTF-8");

结合1楼的

试试
youjianbo_han_87 2009-11-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fireelf000 的回复:]
要写成这样eval( "(" + xmlHttp.responseText + ")" )
[/Quote]

试试。

还有人自己手动写ajax实现,初学写写不错。
fireelf000 2009-11-03
  • 打赏
  • 举报
回复
要写成这样eval( "(" + xmlHttp.responseText + ")" )
junjia 2009-11-03
  • 打赏
  • 举报
回复
function createRequest(){
try{
request=new XMLHttpRequest();
}catch (trymicrosoft){
try{
request=new ActiveXObject("Msxml2.XMLHTTP");
}catch (othermicrosoft){
try{
request=new ActiveXObject("Microsoft.XMLHTTP");
}catch(failed){
request=null;
}
}
}
if(request==null)
alert("Ajax请求错误");
}

function getGuestname(){
var login_name=document.getElementById("login_name").value;
createRequest();
var url="usergetloginname.jsp?login_name="+login_name;
request.open("GET",url,true);
request.setRequestHeader("If-Modified-Since","0");
request.onreadystatechange=updateGuestname;
request.send(null);
}
function updateGuestname(){
if(request.readyState==4){
if(request.status==200){
var guestnames=request.responseText;
var zhuangtai=" ";
var guestw=document.getElementById("guestw");
if(guestnames==1){
zhuangtai="该账号已被占用";
guestw.style.color='red';
}
if(guestnames==0){
zhuangtai=" ";
guestw.style.color='green';
}
guestw.innerHTML=zhuangtai;
}else
alert("服务器无响应");
}
}

usergetloginname.jspjava代码部分
String login_name=request.getParameter("login_name");
Connection conn=null;
Statement st=null;
ResultSet rs=null;
String sql=null;
String guest="0";
PrintWriter outv = response.getWriter();
try{
conn = DBUtil.getConn("sjj");
st=conn.createStatement();
sql="select * from tbl_user where login_name='"+login_name+"'";
rs=st.executeQuery(sql);
if(rs.next()){
guest="1";
}
}catch(Exception e) {
//System.out.println(e);
}finally{
if(rs!=null)
rs.close();
if(st!=null)
st.close();
if(conn!=null)
conn.close();
}
outv.println(guest);
outv.close();
高高 2009-11-03
  • 打赏
  • 举报
回复
大哥是这样的。var jsonObj=eval("("xmlHttp.responseText")");
另外json字符串要写对。这个很关键
lookhang 2009-11-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wula0010 的回复:]
不想用jquery,因为不自由,这个方便。
[/Quote]

有什么不自由的???请楼主指教~

你这个就是$.ajax(.....),连基本的返回JSON都搞不定,还要说方便~
wula0010 2009-11-03
  • 打赏
  • 举报
回复
不想用jquery,因为不自由,这个方便。
我晕死哦 2009-11-03
  • 打赏
  • 举报
回复
为什么不用框架?比如JQuery之类的。。

81,092

社区成员

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

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