zl程序教程

您现在的位置是:首页 >  前端

当前栏目

easyui webapi

2023-09-11 14:21:23 时间

今天算是踩雷了。。。。

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

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

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

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

我给大家看一下问题:


html--webapi

$('#tt').datagrid({
        width: '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', width: 180 },
            { field: 'PT_CreateTime', title: 'PT_CreateTime', align: 'center', width: 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({
        width: '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', width: 180 },
            { field: 'PT_CreateTime', title: 'PT_CreateTime', align: 'center', width: 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