当前位置: 代码迷 >> Java Web开发 >> js无法获取解析jsp中的json字符串,生手不理解
  详细解决方案

js无法获取解析jsp中的json字符串,生手不理解

热度:558   发布时间:2016-04-14 21:05:33.0
js无法获取解析jsp中的json字符串,新手不理解。
文件:data.jsp   
 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
    <%  
        String path = request.getContextPath();  
        String basePath = request.getScheme() + "://"  
                + request.getServerName() + ":" + request.getServerPort()  
                + path + "/";  
    %>  
    <html>  
        <head>  
            <base href="<%=basePath%>">  
            <link rel="stylesheet" type="text/css" href="core/css/ext-all.css" />  
            <script type="text/javascript" src="core/ext-all.js"></script>  
            <script type="text/javascript" src="app/hello.js"></script>  
        </head>  
        <body>  
        </body>  
    </html>  

文件:hello.js
 Ext.onReady(function() {  
 

 
         var store = new Ext.data.JsonStore({  
          proxy:new Ext.data.HttpProxy({url:'json.jsp'}),
             autoLoad: true,  
             fields : [ {
                 name : 'CJ'
             }, {
                 name : 'CQLG_TJSJ'
             }, {
                 name : 'KCDM'
             }, {
                 name : 'KCMC'
             },  {
                 name : 'OPERATE_TIME'
               },{
                 name : 'XH'
               }, {
                 name : 'XKKH'
               },{
                 name : 'XM'
               }, {
                 name : 'XN'
               }, {
                 name : 'XQ'
               }, {
                 name : '事件类型'
               }, {
                 name : '客户端IP'
               }, {
                 name : '操作用户'
               }, {
                 name : '数据库对象'
               }, {
                 name : '数据库对象类型'
               }, {
                 name : '数据类型'
               }],
             listeners : {  
                 load : function(store,records,options) {  
                    alert("数据成功加载");  
                 }  
             }  
         });  
              
            var grid = new Ext.grid.GridPanel({ 
                     flex:100,
                     height:500,
                        store : store,  
                        columns : [{
                            header : '成绩',
                            dataIndex : 'CJ',
                            flex:5.8565
                       } ,{
                            header : '提交时间',
                            dataIndex : 'CQLG_TJSJ',
                            flex:5.8565
                        } ,{
                            header : '课程代码',
                            dataIndex : 'KCDM',
                            flex:5.8565
                        } , {
                            header : '课程名称',
                            dataIndex : 'KCMC',
                            flex:5.8565
                       }, {
                            header : '操作时间',
                            dataIndex : 'OPERATE_TIME',
                            flex:5.8565
                        } , {
                             header : '学号',
                             dataIndex : 'XH',
                             flex:5.8565
                       } , {
                           header : '选课课号',
                           dataIndex : 'XKKH',
                           flex:5.8565
                       },{
                            header : '姓名',
                            dataIndex : 'XM',
                            flex:5.8565
                       },{
                         header : '学年',
                         dataIndex : 'XN',
                         flex:5.8565
                       }, {
                         header : '学期',
                         dataIndex : 'XQ',
                         flex:5.8565
                       },{
                            header : '事件类型',
                            dataIndex : '事件类型',
                            flex:5.8565
                        } ,{
                            header : '客户端IP',
                            dataIndex : '客户端IP',
                            flex:5.8565
                        },{
                            header : '操作用户',
                            dataIndex : '操作用户',
                            flex:5.8565
                        } ,{
                            header : '数据库对象',
                            dataIndex : '数据库对象',
                            flex:5.8565
                        } ,{
                            header : '数据库对象类型',
                            dataIndex : '数据库对象类型',
                            flex:8.4188
                        } ,{
                         header : '数据类型',
                         dataIndex : '数据类型',
                         flex:5.8565
                       }],
                        stripeRows : true,  
                        autoExpandColumn : 'name',  
                        height : 500,  
                        width : 600,  
                        title : '学生成绩数据异常变动情况',  
                        stateful : true,  
                        stateId : 'grid'  
                    });  
  
            var win = new Ext.Window({  
                        flex : 100,  
                        height : 450,  
                        title : '成绩管理系统',  
                        items : [grid],  
                        buttons : [{  
                                    text : '1',  
                                    handler : function() {  
                                        // insert()  
                                     alert('新增');
                                    }  
                                }, {  
                                    text : '2',   
                                    handler : function() {  
                                        datadelete('删除');  
                                    }  
                                }, {  
                                    text : '刷新',  
                                    handler : function() {  
                                        // debugger;  
                                        store.load();  
                                    }  
                                }]  
                    });
            win.show();  
        }); 


原文件json.jsp
 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="jsonEngine.cjb_show_info"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>获取字符串</title>
</head>
<body>
<%  
    String path = request.getContextPath();  
    String basePath = request.getScheme() + "://"  
            + request.getServerName() + ":" + request.getServerPort()  
            + path + "/";  
    System.out.println(basePath);
%> 
<%
         cjb_show_info cjb=new cjb_show_info();
         cjb.csi();
         String tt=cjb.dataStr;
         out.write(tt);
         
        response.setContentType("application/x-json");
        response.getWriter().write(tt);
        System.out.println(tt);
        
%>

</body>

</html>



各位前辈多指教指教
------解决思路----------------------
不知道你想问什么,开始学json建议别用第三方插件,自己写json格式去解析,对于原理的理解很有帮助
------解决思路----------------------
把你的单引号全换成双引号。
------解决思路----------------------
又是你。。。
我严重的说一句,Jsp页面里不要出现Java代码,这是非常糟糕的

Ext对Json的格式要求不严格 单引号也可以的
  相关解决方案