ext 如何远程读取json数据

山城忙碌人 2011-08-04 10:00:17
ext js代码如下

Ext.onReady(function() {


// 创建远程代理
var httpProxy = new Ext.data.HttpProxy({
url : "/test/groupGridController.js"
});

var record = new Ext.data.Record.create([{
name : "studentid"
}, {
name : "studentname"
}, {
name : "birthday"
}, {
name : "department"
}, {
name : "professional"
}])

// 创建解析方式
var jsonReader = new Ext.data.JsonReader({
totalProperty : 'results',
root : 'items'
}, record);

// json分组数据源
var jsonStore = new Ext.data.Store({
reader : jsonReader,
proxy : httpProxy,
autoLoad : true
})

jsonStore.on('load', function() {
alert("数据加载完成:" + jsonStore.getCount());,这里一直没执行到,请问是什么原因
});
// 创建列解析模型
var columns = new Ext.grid.ColumnModel({
defaults : {
sortable : true
},
columns : [{
header : '编号',
dataIndex : 'studentid'
}, {
header : '姓名',
dataIndex : 'studentname'
}, {
header : '出生日期',
dataIndex : 'birthday',
type : 'date',
renderer : Ext.util.Format.dateRenderer('Y年m月d日')
}, {
header : '系部',
dataIndex : 'department'
}, {
header : '专业',
dataIndex : 'professional'
}]
});
var grid = new Ext.grid.GridPanel({
title : '表格分组一',
renderTo : "groupGrid",
autoHeight : true,
width : 600,
cm : columns,
store : jsonStore
});
});

输出json的是jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'groupGridController.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%
String resultJson = "{'results':1,'items':[{'birthday':'1985-05-09','department':'机械','professional':'车工','studentid':'1','studentname':'张三'}]}";
response.setCharacterEncoding("utf-8");
out.print(resultJson);
System.out.println(resultJson);
%>
</body>
</html>

jsp输出的json字符串如下:
{'results':1,'items':[{'birthday':'1985-05-09','department':'机械','professional':'车工','studentid':'1','studentname':'张三'}]}

请问红色部分为什么没有值。
而且表格里面也加载不了数据。解决后立即给分
...全文
189 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
山城忙碌人 2011-08-06
  • 打赏
  • 举报
回复
fanchuanzhidu
hookee

感谢你们两个的提醒。。谢了。。。结帖子,给你们分。。
keithcai 2011-08-05
  • 打赏
  • 举报
回复

Ext.Ajax.request({
url: 'XXX.jsp',
// method:'GET', //默认为POST
params: {userId:_curUserId}, //这个传值,注意JSON格式
success: function(response){
alert(Ext.decode(response.responseText).msg); //解析接受JSON
}
});

Ext.decode(response.responseText)用来解析JSON这个应该是EXTJS4的方法~
之前的好像是Ext.util.JSON.decode(response.responseText)
被解析的JSON请按照以下格式
{success:,msg:,data:[{id:,name:}]}
想解析data的id就Ext.decode(response.responseText).data.id
明白了吗?
hookee 2011-08-05
  • 打赏
  • 举报
回复
jsp只要输出json数据本身,不能包含<html>之类的非js代码
豆虫 2011-08-04
  • 打赏
  • 举报
回复
jsonStore.load({  
params: {start:0, limit:30},
callback: function(r, options, success){
if(success){
Ext.Msg.alert('操作','成功!');
}else{
Ext.Msg.alert('操作','失败!');
}
}
});

这样来写

87,922

社区成员

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

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