怎样通过extjs从一个jsp页面读取一个json字符串的内容?

我想用EXTJS实现把从一个JSP页面上的json字符串,在EXT的页面上显示,为什么总是得不到JSP页面上的内容。在JSP页面上这样写的:jsonText.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>

<body>
<%out.print("[{name:'aaa',age:28,sex:'女'},{name:'bbb',age:28,sex:'男子'},{name:'ccc',age:30,sex:'女'}]");%>
</body>
</html>
EXT页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link type="text/css" rel="stylesheet" href="../../extjs/resources/css/ext-all.css">
<script type="text/javascript" src="../../extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../extjs/ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function(){
var _store=new Ext.data.JsonStore({
proxy:new Ext.data.HttpProxy({
url:"localhost:8080/dag/jsonTest.jsp"

}),
fields:["name","age","sex"]
});
_store.on("load",function(_store){
alert("hi");
alert(_store.getCount());
alert(Ext.util.JSON.encode(_store.getAt(0).data));
});
_store.load();

});
</script>
</head>
<body>

</body>
</html>
文件路径是对的,直接通过服务器能看到内容,但是通过EXT就是读不到任何内容,为什么在EXT页面上总是加载不到JSP页面的内容?请大家帮帮忙,给看一下是哪的问题?一定给加分。
最新回答
晚街

2024-11-03 13:45:31

我记得我曾经也遇到过,好像是这样解决的,把jsp中除了<%%>以外的内容都删掉。
对于你这个 jsonText.jsp 而言
最后就留下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%out.print("[{name:'aaa',age:28,sex:'女'},{name:'bbb',age:28,sex:'男子'},{name:'ccc',age:30,sex:'女'}]");%>

如果不行的话,你改成
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-store"); //HTTP1.1
response.setHeader("Pragma", "no-cache"); //HTTP1.0
response.setDateHeader("Expires", 0); //prevents catching at proxy server
response.getWriter().write("[{name:'aaa',age:28,sex:'女'},{name:'bbb',age:28,sex:'男子'},{name:'ccc',age:30,sex:'女'}]");
%>
你试试
云散风尽独留

2024-11-03 02:17:31

url:"localhost:8080/dag/jsonTest.jsp"
不用localhost:8080这样的,也许你可以选择Ext,Ajax.request()来加载