jqGrid的不显示从ASMX web服务数据(vb.net) [英] jqGrid does not display data from asmx web service (vb.net)

查看:361
本文介绍了jqGrid的不显示从ASMX web服务数据(vb.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的jQuery / jqGrid的。我想从ASMX Web服务中的jqGrid返回JSON格式的数据并显示数据。网格显示在网页上,但它不包含任何数据的行。我不知道如果我返回该jqGrid的是寻找正确的格式。或者,如果我失去了别的东西。

(我遇到的许多问题上的SO与此相关的话题,所以我提前道歉,如果这已经回答了。在这一点上,可用不同的答案连号是造成进一步的混乱)。

Web服务

 < System.Web.Script.Services.ScriptService()> _
    < WebService的空间(namespace:=htt​​p://tempuri.org/)> _
    < WebServiceBinding(ConformsTo:= WsiProfiles.BasicProfile1_1)GT; _
    &所述; Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    公共类WebService1
    继承System.Web.Services.WebService    公共类人
        公共名字作为字符串
        公共姓氏作为字符串
    末级    &所述;的WebMethod()>
    < ScriptMethod(UseHttpGet:=假,ResponseFormat:= ResponseFormat.Json)GT; _
    公共职能GetPersonList()方式列表(人)        昏暗personList作为新的列表(人)        连接到数据库        虽然从数据库读取
            personList.Add(
                新的Person(){用
                    .FirstName =博士(其中fname),
                    .LastName =博士(姓氏)
                })        'personList是否需要被转换为不同格式?
        返回personList    结束功能
    末级

aspx页面

 的jQuery(#list1的)。jqGrid的({
            网址:HTTP://localhost/WebService1.asmx/GetPersonList
            数据类型:JSON
            MTYPE:'POST',
            colNames:['姓','名字'],
            colModel:
            {名称:'名字',索引:'名字',宽度:90},
            {名称:'姓氏',索引:'姓氏',宽度:90}
            ]
            的rowNum:10,
            rowList:[10,20,30]
            呼叫器:#pager1',
            viewrecords:真实,
            标题:人名录
        });


解决方案

编辑:使用此功能。人>

我已经使用ASMX服务没有PARAMS返回一个名单,LT测试它

  $(#list1的)。jqGrid的({
    网址:HTTP://localhost/WebService1.asmx/GetPersonList
    数据类型:JSON
    MTYPE:'POST',
    ajaxGridOptions:{的contentType:应用/ JSON的;字符集= UTF-8
    } // ASMX想请求头是application / JSON ..
    serializeGridData:功能(POSTDATA){
        返回null;
    } //分页忽略PARAMS - 如果没有必要(否则你的服务必须定义它们)
    jsonReader:{//奥列格感谢为这个技巧在对付ASMX data.d
        repeatitems:假的,
        根:函数(OBJ){返回obj.d; },
        页:功能(){返回1; },
        总:函数(){返回1; },
        记录:函数(OBJ){返回obj.d.length; }
    },
    colNames:['姓','名字'],
    colModel:
        {名称:'名字',索引:'名字',宽度:90},
        {名称:'姓氏',索引:'姓氏',宽度:90}
        ]
    的rowNum:10,
    rowList:[10,20,30],
    viewrecords:真实,
    标题:人名录
});

另一个有用的技巧是使用像萤火虫设置当你的jqGrid定义一个破发点。你也应该看到在一个呼叫到你的Web服务是否响应是回来还是没有取得Firebug控制台。

另一个有用的技巧是你的包裹在JSON时间戳,状态,消息JsonResponse类,结果..和你需要的任何其他领域(我只用这4个)。

 公共类JsonResponse
{
    公用对象[] {结果得到;组; }
    公共字符串时间戳{搞定;组; }
    公共字符串状态{搞定;组; }
    公共字符串消息{搞定;组; }
}

I'm new to jQuery/jqGrid. I want to return json formatted data from the asmx web service and display the data in the jqGrid. The grid is rendering on the page but it contains no rows of data. I'm not sure if I'm returning the correct format that jqGrid is looking for. Or if I am missing something else.

(I've come across many questions on SO related to this topic so I apologize in advance if this has already been answered. At this point, even the number of different answers available is causing further confusion).

Web Service

 <System.Web.Script.Services.ScriptService()> _
    <WebService(Namespace:="http://tempuri.org/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class WebService1
    Inherits System.Web.Services.WebService

    Public Class Person
        Public FirstName As String
        Public LastName As String
    End Class

    <WebMethod()>
    <ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetPersonList() As List(Of Person)

        Dim personList As New List(Of Person)

        'Connect to DB

        'While reading from DB       
            personList.Add(
                New Person() With {
                    .FirstName= dr("fname"),
                    .LastName = dr("lastName")
                })

        'Does personList need to be converted to a different format?
        Return personList

    End Function
    End Class

ASPX Page

jQuery("#list1").jqGrid({
            url: "http://localhost/WebService1.asmx/GetPersonList",
            datatype: "json",
            mtype: 'POST',
            colNames: ['FirstName', 'LastName'],
            colModel: [
            { name: 'FirstName', index: 'FirstName', width: 90 },
            { name: 'LastName', index: 'LastName', width: 90 }
            ],
            rowNum:10,
            rowList: [10,20,30],
            pager: '#pager1',
            viewrecords: true,
            caption: "Person List"
        });

解决方案

EDIT: Use this. I've tested it using an ASMX service with no params that returns a List<Person>.

$("#list1").jqGrid({
    url: "http://localhost/WebService1.asmx/GetPersonList",
    datatype: "json",
    mtype: 'POST',
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' 
    }, // ASMX wants request header to be application/json..
    serializeGridData: function (postdata) { 
        return null; 
    }, // ignores paging params - if not needed (else your service must define them)
    jsonReader: { // thanks Oleg for this tip on dealing with the ASMX data.d
        repeatitems: false,
        root: function (obj) { return obj.d; },
        page: function () { return 1; },
        total: function () { return 1; },
        records: function (obj) { return obj.d.length; }
    },
    colNames: ['FirstName', 'LastName'],
    colModel: [
        { name: 'FirstName', index: 'FirstName', width: 90 },
        { name: 'LastName', index: 'LastName', width: 90 }
        ],
    rowNum: 10,
    rowList: [10, 20, 30],
    viewrecords: true,
    caption: "Person List"
});

Another helpful tip is to use something like Firebug to set a break point when your jqGrid is defined. You should also see in the Firebug console that a call was made to your web service and whether the response is coming back or not.

Another helpful tip is to wrap your JSON in a JsonResponse class with timestamp, status, message, result .. and any other fields you need (I only ever use those 4).

public class JsonResponse
{
    public object[] Result { get; set; }
    public string TimeStamp { get; set; }
    public string Status { get; set; }
    public string Message { get; set; }
}

这篇关于jqGrid的不显示从ASMX web服务数据(vb.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆