悲催啊,这么简单的程序,.net环境,得不到数据。现象:GridPanel,有表格出现,有表头了,就是没有数据。
html前端
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="ext-4.0.7-gpl/resources/css/ext-all.css" />
    <link rel="stylesheet" type="text/css" href="ext-4.0.7-gpl/examples/shared/example.css" />
    <script type="text/javascript" src="ext-4.0.7-gpl/locale/ext-lang-zh_CN.js"></script>
    <script type="text/javascript" src="ext-4.0.7-gpl/bootstrap.js"></script>
    <script type="text/javascript" language="javascript">
        Ext.onReady(Main);
            function Main() {
                var proxy1 = new Ext.data.HttpProxy({
                    method: 'POST',
                    type: 'ajax',
                    url: 'App_Ashx/Demo/Grid.ashx',
                    reader: {
 
                        type: 'json',
                        root: 'data',
                        totalProperty: 'totalCount',
                        fields: ['Author', 'Title', 'Manufacturer', 'ProductGroup']
 
                    }
                });
                var store1 = Ext.create('Ext.data.Store', {
                    model: 'Book',
                    autoLoad: true,
                    proxy: proxy1
                });
                var grid1 = Ext.create('Ext.grid.Panel', {
                    store: store1,
                    columns: [
                            { text: "作者", flex: 1, dataIndex: 'Author', sortable: true },
                            { text: "书名", width: 180, dataIndex: 'Title', sortable: true },
                            { text: "出版商", width: 115, dataIndex: 'Manufacturer', sortable: true },
                            { text: "商品类别", width: 100, dataIndex: 'ProductGroup', sortable: true }
                        ],
                    renderTo: 'example-grid1',
                    width: 540,
                    height: 200
                });
            }
       
    </script>
</head>
<body>
    <div id="example-grid1"></div>
</body>
</html>.net后台ashx程序:
<%@ WebHandler Language="C#" Class="Grid" %>using System;
using System.Web;public class Grid : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";        context.Response.Write("[totalCount:2,data:[{Author:wys,Title:wys,Manufacturer:wys,ProductGroup:Book},{Author:tangchun,Title:Christian,Manufacturer:google,ProductGroup:Book}]]");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }}
iis的webconfig:
<?xml version="1.0" encoding="UTF-8"?><!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  --><configuration>
    <connectionStrings>
        <add name="csmConnectionString" connectionString="Data Source=localhost;Initial Catalog=CSMPlatForm;Persist Security Info=True;User ID=sa;Password=m9e8f2i1ABC@#$" providerName="System.Data.SqlClient" />
    </connectionStrings>
  <location path="ASHXPRO">
    <system.web>
      <authorization>
        <allow users="*"/>
      
      </authorization>
    </system.web>
  </location>  
  <system.web>
    <!--加入支持webservices功能-->
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
    <pages validateRequest="false" enableEventValidation="false" enableViewStateMac="false" />
    
    <compilation debug="false" targetFramework="4.0" />
        <sessionState mode="StateServer" stateConnectionString="tcpip=WIN-HVQ7K2NRMMS:42424" timeout="3"/>    
        <!--<authentication mode="Forms">
            <forms loginUrl="Default.aspx" timeout="3" defaultUrl="~/Contact/Users.aspx" cookieless="UseCookies" name=".ASPXFORMSAUTH" />
        </authentication>
        <authorization>
            <deny users="?" />
        </authorization>-->
    <!--<globalization requestEncoding="utf-8" responseEncoding="utf-8" />-->
    </system.web>
    <system.webServer>
        <defaultDocument>
            <files>
                <clear />
                <add value="default.aspx" />
                <add value="login.aspx" />
                <add value="Default.htm" />
                <add value="Default.asp" />
                <add value="index.htm" />
                <add value="index.html" />
                <add value="iisstart.htm" />
            </files>
        </defaultDocument>
    </system.webServer></configuration>

解决方案 »

  1.   

    context.Response.Write("{totalCount:2,data:[{Author:wys,Title:wys,Manufacturer:wys,ProductGroup:Book},{Author:tangchun,Title:Christian,Manufacturer:google,ProductGroup:Book}]}");
      

  2.   

    输出的时候要输出{}这样可以转化成JS object,然后才可以获得你的root: 'data',
    按照你这个输出它找不到data这个对象
      

  3.   


    我本来是想用颜色突出表达下的,我重新发一遍吧"[totalCount:2,data:[{Author:wys,Title:wys,Manufacturer:wys,ProductGroup:Book},{Author:tangchun,Title:Christian,Manufacturer:google,ProductGroup:Book}]]"
    替换成
    "{totalCount:2,data:[{Author:\"wys\",Title:\"wys\",Manufacturer:\"wys\",ProductGroup:\"Book\"},{Author:\"tangchun\",Title:\"Christian\",Manufacturer:\"google\",ProductGroup:\"Book\"}]}"
    目前发现2个问题一个是JS对象是{},还有所有JSON格式字符串 key:value如果value是字符串的话必须要引号引起来,否则它在extjs在转化成JS对象的时候会报错,说wys找不到或者undefined,因为不加引号的情况下它会认为这是一个JS变量
      

  4.   

    第一,ashx输出的JSON字符串内容错误,改成下面的
    context.Response.Write("{totalCount:2,data:[{Author:\"wys\",Title:\"wys\",Manufacturer:\"wys\",ProductGroup:\"Book\"},{Author:\"tangchun\",Title:\"Christian\",Manufacturer:\"google\",ProductGroup:\"Book\"}]}");第二,没有定义Book这个model
     Ext.define('Book', {/////////////
            extend: 'Ext.data.Model',
            fields: ['Author', 'Title', 'Manufacturer', 'ProductGroup']
        });    Ext.onReady(Main);
        function Main() {
            var proxy1 = new Ext.data.HttpProxy({
                method: 'POST',
                type: 'ajax',
                url: 'grid.ashx',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount'
                   // , fields: ['Author', 'Title', 'Manufacturer', 'ProductGroup']//这里可以不用要了            }
            });
           store1 = Ext.create('Ext.data.Store', {
                model: 'Book',
                autoLoad: true,
                proxy: proxy1
            });
      

  5.   

    解决!不过还有一个问题我自己发现了,终于好了,路程艰辛啊!!
    多谢楼上的,两位的回答,还是很有必要的。另外,web.config的httphandler修改一下,终于出来数据了!哎!!不堪回首,两天班啊,周末都没有过好。
    在system.web节里面:
    <httpHandlers>
          <!--<add verb="POST,GET" path="App_Ashx/Demo/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>-->
          <add path="App_Ashx/Demo/*.ashx" verb="POST,GET" type="System.Web.UI.SimpleHandlerFactory" validate="true" />
    </httpHandlers>
    原来写的上面的,AjaxPro.AjaxHandlerFactory,后来换成下面的了,不知道这是为什么。我对程序深层次的不太了解。反正好了!谢谢!