zoukankan      html  css  js  c++  java
  • easyui webapi

    今天算是踩雷了。。。。

    先说一下,由于项目需要,我目前开发PO模块,

    由于需要提供手机端,所以我在mvc项目中创建了  webapi。提供手机端调用。

    然后我就考虑,easyui也使用webapi来提取数据。

    好来,那么问题来了。。。。

    我给大家看一下问题:


    html--webapi

    $('#tt').datagrid({
             'auto',
            height: 300,
            striped: true,
            singleSelect: true,
            method: 'Get',
            url: 'Test/Get',
            loadMsg: '数据加载……',
            pagination: true,
            rownumbers: true,
            columns: [[
                { field: 'PT_Name', title: 'PT_Name', align: 'center',  180 },
                { field: 'PT_CreateTime', title: 'PT_CreateTime', align: 'center',  180 }
    
            ]]
        });
    返回的数据:

    "{"total": 1,"rows":[{"PT_ID":1,"PT_ParentID":0,"PT_Name":"鞋子","PT_Code":"Shoes","CompanyInfo_ID":null,"PT_CreateTime":null},{"PT_ID":2,"PT_ParentID":0,"PT_Name":"dfaz","PT_Code":"asfaf","CompanyInfo_ID":null,"PT_CreateTime":null}]}"


    在看看html--ashx

    $('#tt').datagrid({
             'auto',
            height: 300,
            striped: true,
            singleSelect: true,
            method: 'Get',
            url: '/Ashx/Handler1.ashx',
            loadMsg: '数据加载……',
            pagination: true,
            rownumbers: true,
            columns: [[
                { field: 'PT_Name', title: 'PT_Name', align: 'center',  180 },
                { field: 'PT_CreateTime', title: 'PT_CreateTime', align: 'center',  180 }
    
            ]]
        });
    返回的数据:

    {"total": 1,"rows":[{"PT_ID":1,"PT_ParentID":0,"PT_Name":"鞋子","PT_Code":"Shoes","CompanyInfo_ID":null,"PT_CreateTime":null},{"PT_ID":2,"PT_ParentID":0,"PT_Name":"dfaz","PT_Code":"asfaf","CompanyInfo_ID":null,"PT_CreateTime":null}]}

    细心的你,可能已经发现了、webapi会在最外面带双引号。导致easyui无法解析json!!!


    后来的解决方案:

    1  使用HttpResponseMessage 返回(这里还是使用webapi来返回,如果使用ashx,那么直接前面的代码就能搞定了)
    public HttpResponseMessage Get()
            {
                string a = "{"total": 1,"rows":[{"PT_ID":1,"PT_ParentID":0,"PT_Name":"鞋子","PT_Code":"Shoes","CompanyInfo_ID":null,"PT_CreateTime":null},{"PT_ID":2,"PT_ParentID":0,"PT_Name":"dfaz","PT_Code":"asfaf","CompanyInfo_ID":null,"PT_CreateTime":null}]}";
                var resp = new HttpResponseMessage { Content = new StringContent(a, System.Text.Encoding.UTF8, "application/json") };
    
                return resp;
            }

    2  使用对象返回

    public Rootobject Get()
            {
                Rootobject resp=new Rootobject();
                //省略赋值
    
                return resp;
            }
    
            public class Rootobject
            {
                public int total { get; set; }
                public List<Row> rows = new List<Row>();
            }
    
            public class Row
            {
                public int PT_ID { get; set; }
                public int PT_ParentID { get; set; }
                public string PT_Name { get; set; }
                public string PT_Code { get; set; }
                public object CompanyInfo_ID { get; set; }
                public object PT_CreateTime { get; set; }
            }

    没有想到这么坑爹。。。。哎。记录一下,给大伙提个醒



    ashx和 webapi都是

    返回

    string a = "{"total": 1,"rows":[{"PT_ID":1,"PT_ParentID":0,"PT_Name":"鞋子","PT_Code":"Shoes","CompanyInfo_ID":null,"PT_CreateTime":null},{"PT_ID":2,"PT_ParentID":0,"PT_Name":"dfaz","PT_Code":"asfaf","CompanyInfo_ID":null,"PT_CreateTime":null}]}";


    也是研究不够深入,哎。后来才想起来。。。

    不同的是,ashx其实通过HttpResponse返回,而webapi则直接返回 string





  • 相关阅读:
    删除顺序表L中下标为p(0<=p<=length-1)的元素,成功返回1,不成功返回0,并将删除元素的值赋给e
    设顺序表中的数据元素递增有序,试着写一算法,将x插入到顺序表上的适当位置上,以保持该表的有序性。
    数据结构-顺序表基本操作的实现(含全部代码)【转】
    【转】结构体指针
    结构体(第十四章 )
    线性表
    第二章 c语言概述
    时间复杂度
    软件质量与测试 黑盒测试
    软件质量保证与测试 基本内容
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/9779894.html
Copyright © 2011-2022 走看看