我有一个 JSP 文件,我在其中声明了一个返回 json 的方法。现在我想使用 ajax 在 js 文件中获取这个 json。但是当我使用ajax调用它返回整个jsp页面而不是json数据这里是我的jsp
<%--
Document : index
Created on : Jul 26, 2012, 3:49:33 PM
Author : tagit
--%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="my.mychartData"%>
<%@page import="my.mychartInfo"%>
<%@page import="my.mychart"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
</script>
</head>
<body>
<%!
Object showchart()
{
mychartInfo c1 =new mychartInfo();
mychartInfo c2=new mychartInfo();
mychartInfo c3=new mychartInfo();
mychartInfo c4=new mychartInfo();
mychartInfo c5=new mychartInfo();
mychartInfo c6=new mychartInfo();
mychartData c7=new mychartData();
c1.setColor("pink");
c1.setDataDes("Gas data");
c1.setDataVal("20");
c1.setName("Gas");
c2.setColor("green");
c2.setDataDes("cash data");
c2.setDataVal("30");
c2.setName("Cash");
c3.setColor("blue");
c3.setDataDes("Resturent data");
c3.setDataVal("38");
c3.setName("Resturent");
c4.setColor("blue");
c4.setDataDes("boat data");
c4.setDataVal("60");
c4.setName("Boat");
c5.setColor("black");
c5.setDataDes("Movies data");
c5.setDataVal("40");
c5.setName("Movies");
c6.setColor("yello");
c6.setDataDes("Shopping data");
c6.setDataVal("50");
c6.setName("Shopping");
c7.setDonut("30");
c7.setFont("afdafafafafaf");
c7.setGradientPos("leftsdgd");
mychart chart1=new mychart();
Object obj =chart1.addChart(c1, c2, c3, c4, c5, c6, c7);
return obj ;
}
%>
<%
out.println( showchart());
%>
%>
</body>
</html>
这是我的ajax函数
$.ajax({
type: "get"
,dataType: "json"
, url: "http://localhost:8080/Webcharts/index.jsp;"
,success: function(json){
alert("here"+json);
}
});