使用ASMX服务的Kendo UI Grid:返回XML而不是JSON

时间:2013-12-11 02:15:04

标签: kendo-ui asmx

我使用以下Kendo UI Grid来使用下面的ASMX服务。我已经确认,如果我使用标准的JQuery ajax方法,该服务正在发出JSON,但如果我尝试使用kendo网格消耗它,那么我会将此作为响应:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfEmployeeCountByTypeModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:nil="true" xmlns="http://tempuri.org/" />


$("#grid").kendoGrid({
        dataSource: {
            type: "json",
            transport: {
                read: {
                    type: "POST",
                    dataType: "json",
                    url: "HRDashboardService.asmx/GetEmployeeCountByType",
                },
                contentType: "application/json; charset=utf-8"
            },
            schema: {
                data: "d",
                model: {
                    fields: {
                        FY: { type: "string" },
                        Month: { type: "string" },
                        AreaName: { type: "string" },
                        PFCName: { type: "string" },
                        OnRoll: { type: "number" }
                    }
                }
            }
        },
        groupable: false,
        sortable: true,
        pageable: {
            refresh: true,
            pageSizes: true
        },
        columns: [{
            field: "FY",
            width: 20,
            title: "FY"
        },
        {
            field: "Month",
            width: 20,
            title: "Month"
        },
        {
            width: 20,
            field: "AreaName",
            title: "Area Name"
        },
        {
            width: 20,
            field: "PFCName",
            title: "PFC Name"
        },
        {
            field: "EmployeeType",
            width: 40,
            title: "Employee Type"
        },
        {
            width: 20,
            field: "OnRolls",
            title: "OnRolls"
        }]
    });

这是我的ASMX服务:

<WebMethod()> _
Public Function GetEmployeeCountByType() As List(Of EmployeeCountByTypeModel)
    Dim results As List(Of EmployeeCountByTypeModel) = Nothing

    Try
        results = (From r In DbContext.SprocEmployeeCountByType
                       Select New EmployeeCountByTypeModel With { _
                            .AreaCode = r.AreaCode, _
                            .FY = r.FY, _
                            .AreaName = r.AreaName, _
                            .EmployeeType = r.EmployeeType, _
                            .Month = r.Month, _
                            .OnRoll = r.OnRoll, _
                            .PFCCode = r.PFCCode, _
                            .PFCName = r.PFCName _
                        }).ToList()
    Catch ex As Exception

    End Try
    Return results
End Function

2 个答案:

答案 0 :(得分:1)

我也在努力解决这个问题,无法得到答案。 这就是我发现的。 如果您仍然遇到问题......

以下代码行必须在webservice中取消注释:

<System.Web.Script.Services.ScriptService()> _

请务必在

中的webservice的web.config文件中添加以下内容
<webServices>
   <protocols>
      <add name="HttpGet"/>
      <add name="HttpPost"/>
   </protocols>
</webServices>

创建一个这样的类,它将包含项目列表和行数:

Public Class UsersResult
    Private _Data As List(Of clsUsers_Item)
    Private _Total As Integer

    Public Sub New()
        _Data = Nothing
        _Total = 0
    End Sub

    Public Sub New(data As List(Of clsUsers_Item), total As Integer)
        _Data = data
        _Total = total
    End Sub

    Public Property Total() As Integer
        Get
            Return _Total
        End Get
        Set(ByVal value As Integer)
            _Total = value
        End Set
    End Property


    Public Property Data() As List(Of clsUsers_Item)
        Get
            Return _Data
        End Get
        Set(ByVal value As List(Of clsUsers_Item))
            _Data = value
        End Set
    End Property
End Class

和webservice的代码:

<WebMethod(), ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function getUsers() As UsersResult
    'data will be the list of objects that you want to return
    Return New UsersResult(data, data.Count)
End Function

答案 1 :(得分:0)

使用google开发人员工具的网络标签确保kendo网格数据源确实将请求作为POST类型发送而不是GET。

此链接描述了为什么asmx服务可能返回xml而不是json ... How to let an ASMX file output JSON