不知道怎么出不来,又不报错
执行到JSONObject jsonobj=new JSONObject(data.toString());时后面的就不执行了!!
大虾帮忙看下Java文件(servlet):
PostData.javaimport java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.json.JSONObject;public class PostData extends HttpServlet
{
protected void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
doPost(req,res);
}
protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
StringBuffer data=new StringBuffer();

String temp=null;
try
{
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
BufferedReader reade=req.getReader();
while((temp=reade.readLine())!=null)
{
data.append(temp);
}
System.out.println(temp);
JSONObject jsonobj=new JSONObject(data.toString());
System.out.println("ok");
res.setContentType("text/xml");
res.getWriter().print("您的信息: 名字:"+jsonobj.getString("name")+",年龄:"+jsonobj.getInt("age")+",性别:"+jsonobj.getString("sex")+",喜欢的颜色:"+jsonobj.getString("loveColor")+".");
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}HTML文件 test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0  Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>动态查询显示结果
</title>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript">
var xmlHttp;
function createXMLHTTPRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}
function doJson()
{
createXMLHTTPRequest();
var url="PostData?timeStamp="+(new Date()).getTime();
//var json=JSON.stringify(new People("唐俊","21","男","蓝色"));
var carAsJson=JSON.stringify(new People("aa","21","b","blue"));

//alert(JSON.stringify(new People("唐俊","21","男","蓝色")));
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(carAsJson);
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
alert(xmlHttp.responseText);
document.getElementById("selectdata").innerHTML=xmlHttp.responseText;
}
}
}
function People(name,age,sex,loveColor)
{
this.name=name;
this.age=age;
this.sex=sex;
this.loveColor=loveColor;
}
</script>
</head>
<body>
<form action="#">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="text" name="sex"></td>
</tr>
<tr>
<td>你喜欢的颜色:</td>
<td><input type="text" name="loveColor"></td>
</tr>
</table>
<input type="button" value="GetData" onclick="doJson();"/><br><br>

<div id="selectdata"></div>
</form>
</body>
</html>多谢了!