AJAX结合JSON在火狐下可以,但是IE下不行

nquanhao001 2012-10-10 09:10:58
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript">
var xmlHttp;
function doJson()
{
var cars = new car("奔驰","SLCAR00004500",50000);
var carAsJson=JSON.stringify(cars);
alert("Car object as json:\n"+carAsJson);

var url ="JSONExample?timeStamp="+new Date().getTime();
createXMLHttpRequest();
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(carAsJson);
}

function car(carname,carno,carprice)
{
this.carName=carname;
this.carNo=carno;
this.carPrice=carprice;
}

function createXMLHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}

function handleStateChange()
{
if(xmlHttp.readyState ==4 && xmlHttp.status==200)
{
parseResult();
}
}

function parseResult()
{
var responseDiv = document.getElementById("serverResponse");
if(responseDiv.hasChildNodes())
{
responseDiv.removeChild(responseDiv.childNodes[0]);
}
var responseText = document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
}
</script>
</head>

<body>
<form action="#">
<input type="button" name="button" value="show Json" onclick="doJson()" />
</form>
<h2>Server response:</h2>
<div id="serverResponse"></div>
</body>
</html>

package com.suning.study.ajax.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

@SuppressWarnings("serial")
public class AjaXDemo extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String json = readJsonStringFromRequest(request);
JSONObject obj=null;
String responseText ="";
try {
obj=new JSONObject(json);
responseText="The car is a car[carName:"+obj.getString("carName")+",carNo:"+obj.getString("carNo")
+",carPrice:"+obj.getDouble("carPrice")+"]";
} catch (JSONException e) {
e.printStackTrace();
}

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print(responseText);
}


private String readJsonStringFromRequest(HttpServletRequest request) {
StringBuffer buffer = new StringBuffer();
String line = "";
try {
BufferedReader reader = request.getReader();
while ((line=reader.readLine())!=null) {
buffer.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}

}


望大侠们帮小弟看看
...全文
182 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
nquanhao001 2012-10-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

var carAsJson=JSON.stringify(cars);

IE浏览器没有JSON对象,需要自己编写函数序列化JSON对象。

参考这个:在ie下将json对象转化成json字符串的问题

json.js下载,已经修正和jq的冲突
[/Quote]
不错,果然解决了
  • 打赏
  • 举报
回复
var carAsJson=JSON.stringify(cars);

IE浏览器没有JSON对象,需要自己编写函数序列化JSON对象。

参考这个:在ie下将json对象转化成json字符串的问题

json.js下载,已经修正和jq的冲突
孟子E章 2012-10-11
  • 打赏
  • 举报
回复
代码没有问题
注意:
1,web.xml要配置
<servlet>
<servlet-name>TTT</servlet-name>
<servlet-class>包名称.AjaXDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TTT</servlet-name>
<url-pattern>访问路径JSONExample要与前端的访问路径一致 var url ="JSONExample?timeStamp="+new Date().getTime(); </url-pattern>
</servlet-mapping>

52,800

社区成员

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

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