Anas
12-11-2009, 09:30 AM
Hi,
When executed the below client code,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<script type="text/javascript" src="/JsonSample/jsp/json2.js"></script>
<script type="text/javascript">
function createXMLHttpRequest(){
if( typeof XMLHttpRequest == "undefined" )
XMLHttpRequest = function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") }catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") }catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP") }catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP") }catch(e) {}
throw new Error( "This browser does not support XMLHttpRequest." )
};
return new XMLHttpRequest();
}
var AJAX = createXMLHttpRequest();
function handler() {
if(AJAX.readyState == 4 && AJAX.status == 200) {
//var json = eval("(" + AJAX.responseText + ")");
var json = JSON.parse(AJAX.responseText);
alert('Success. Result: name => ' + json.name + ',' + 'balance => ' + json.balance);
}
else if (AJAX.readyState == 4 && AJAX.status != 200) {
alert('Something went wrong...');
}
}
function show(){
AJAX.onreadystatechange = handler;
AJAX.open("GET", "service.jsp");
AJAX.send("");
};
</script>
<body>
<a href="#" onclick="javascript:show();"> Click here to get JSON data from the server side</a>
</html>
I am getting "undefined" for the value json as in "var json = JSON.parse(AJAX.responseText);".
The server side code is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="net.sf.json.*"%>
<html>
<head>
<title>service</title>
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<body>
<% JSONObject obj=new JSONObject();
obj.put("name","foo"); obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
out.print(obj);
out.flush(); %>
</body>
</html>
The AJAX.responseText is having the value
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>service</title>
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<body>{"name":"foo","num":100,"balance":1000.21,"is_vip":true}
</body>
</html>
Since json is undefined, i am getting a script error saying "name is null or not an object".
Please help in getting the values server side.
When executed the below client code,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<script type="text/javascript" src="/JsonSample/jsp/json2.js"></script>
<script type="text/javascript">
function createXMLHttpRequest(){
if( typeof XMLHttpRequest == "undefined" )
XMLHttpRequest = function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") }catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") }catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP") }catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP") }catch(e) {}
throw new Error( "This browser does not support XMLHttpRequest." )
};
return new XMLHttpRequest();
}
var AJAX = createXMLHttpRequest();
function handler() {
if(AJAX.readyState == 4 && AJAX.status == 200) {
//var json = eval("(" + AJAX.responseText + ")");
var json = JSON.parse(AJAX.responseText);
alert('Success. Result: name => ' + json.name + ',' + 'balance => ' + json.balance);
}
else if (AJAX.readyState == 4 && AJAX.status != 200) {
alert('Something went wrong...');
}
}
function show(){
AJAX.onreadystatechange = handler;
AJAX.open("GET", "service.jsp");
AJAX.send("");
};
</script>
<body>
<a href="#" onclick="javascript:show();"> Click here to get JSON data from the server side</a>
</html>
I am getting "undefined" for the value json as in "var json = JSON.parse(AJAX.responseText);".
The server side code is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="net.sf.json.*"%>
<html>
<head>
<title>service</title>
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<body>
<% JSONObject obj=new JSONObject();
obj.put("name","foo"); obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
out.print(obj);
out.flush(); %>
</body>
</html>
The AJAX.responseText is having the value
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>service</title>
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<body>{"name":"foo","num":100,"balance":1000.21,"is_vip":true}
</body>
</html>
Since json is undefined, i am getting a script error saying "name is null or not an object".
Please help in getting the values server side.