2013-12-11 71 views
0

我正在使用以下Kendo UI Grid来使用下面的ASMX服务。我已经证实,该服务emmitting JSON如果我使用一个标准的jQuery AJAX方法,但如果我尝试用剑道电网消耗它,然后我得到这个作为响应:带有ASMX服务的Kendo UI Grid:返回XML而不是JSON

<?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 

回答

0

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

以下链接介绍了为什么ASMX服务可能返回XML而不是JSON的...... How to let an ASMX file output JSON

+0

是的,我已经证实,它使用的是POST不是GET。 – Asmussen

1

我也努力与此,无法得到答案。 这就是我发现的。 如果您仍然有,这是问题...

下面的代码行必须在web服务的注释去掉:

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

一定要添加下面的的web.config文件Web服务内部

<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 

而且Web服务的代码:

<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 
+0

欢迎来到SO!这是一个很好的答案! –

相关问题