繁体   English   中英

在调用启用AJAX的WCF服务之前修改jqGrid的postData

[英]Modify the postData of jqGrid before call to AJAX-enabled WCF Service

我有一个启用 AJAX 的 WCF 服务,其签名如下:

       [OperationContract]
       [WebGet]
       public JQGridContract GetJQGrid(int entityIndex)

以及以下数据合同:

[DataContract]
public class JQGridContract
{
    [DataContract]
    public class Row
    {
        [DataMember]
        public int id { get; set; }

        [DataMember]
        public List<string> cell { get; set; }

        public Row()
        {
            cell = new List<string>();
        }
    }

    [DataMember]
    public int page { get; set; }

    [DataMember]
    public int total { get; set; }

    [DataMember]
    public int records { get; set; }

    [DataMember]
    public List<Row> rows { get; set; }

    public JQGridContract()
    {
        rows = new List<Row>();
    }
}  

基本上我需要更改客户端 jqGrid 的 postData 以将“entityIndex”发送到该服务。

我已经阅读了它应该如何 function 并且据我所知这应该可以工作:

 function loadGrid() {

    $("#jqGrid").jqGrid({

        postData: { entityIndex : function () {    // modify the data posted to AJAX call here

            return 6;   

          })
        },
        gridComplete: function () {

            $("#jqGrid").setGridParam({ datatype: 'local' });
        },
        datatype: function (pdata) {
            getData(pdata);
        },

这是 getData() function:

  function getData(pdata) {

    var params = new Object();

    alert(pdata.entityIndex());               // this displays '6', correctly

    params.entityIndex = pdata.entityIndex(); 


    $.ajax(
            {
                type: "GET",
                contentType: "application/json; charset=utf-8",
                url: "AJAXService.svc/GetJQGrid",
                data: JSON.stringify(params),
                dataType: "json",
                success: function (data, textStatus) {
                    if (textStatus == "success") {
                        var thegrid = $("#jqGrid")[0];

                        thegrid.addJSONData(data.d);
                    }
                },
                error: function (data, textStatus) {
                    alert('An error has occured retrieving data!');
                }
            });

我在 Firebug 中确认了以下内容:

1) json 参数正确:{"entityIndex":6}

2) AJAX 服务向网格返回 JSON 数据,它只是错误的数据

这是奇怪的部分:

我记录了在 WCF 操作中实际工作的“实体索引”——它总是显示为 0?

谢谢。

我不会批评你程序的风格。 我可以写太多关于这个的东西。 :-)

您当前的主要问题可以通过使用JSON.stringify(pdata.entityIndex())而不是JSON.stringify(params)或使用 WFC 方法的另一种BodyStyle来解决(详见此处

我得到它的工作,它接近奥列格所说的,只是你不需要做 JSON.stringify。

如果你有 WebMessageBodyStyle.WrappedRequest,这可行:

data: { entityIndex: pdata.entityIndex() },   

或者,如果您没有 BodyStyle,这可行:

data: { "entityIndex": pdata.entityIndex() },  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM