简体   繁体   English

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

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

I have an AJAX-enabled WCF service with the following signature:我有一个启用 AJAX 的 WCF 服务,其签名如下:

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

And the following data contract:以及以下数据合同:

[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>();
    }
}  

Basically I need to change the postData of the client-side jqGrid to send 'entityIndex' to this service.基本上我需要更改客户端 jqGrid 的 postData 以将“entityIndex”发送到该服务。

I've read how its supposed to function and from what I can tell this should work:我已经阅读了它应该如何 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);
        },

And here is the getData() function:这是 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!');
                }
            });

Ive confirmed the following in Firebug:我在 Firebug 中确认了以下内容:

1) The json params are correct: {"entityIndex":6} 1) json 参数正确:{"entityIndex":6}

2) The AJAX service returns JSON data to the grid, its just the wrong data 2) AJAX 服务向网格返回 JSON 数据,它只是错误的数据

And here is the wierd part:这是奇怪的部分:

I logged the 'entityIndex' thats actually working inside the WCF operation -- and its ALWAYS coming up as 0?我记录了在 WCF 操作中实际工作的“实体索引”——它总是显示为 0?

Thanks.谢谢。

I will not criticize the style of your program.我不会批评你程序的风格。 I could write too many things about this.我可以写太多关于这个的东西。 :-) :-)

You current main problem could be solved with the usage JSON.stringify(pdata.entityIndex()) instead of JSON.stringify(params) or with the usage of another BodyStyle of the WFC method (see here for details)您当前的主要问题可以通过使用JSON.stringify(pdata.entityIndex())而不是JSON.stringify(params)或使用 WFC 方法的另一种BodyStyle来解决(详见此处

I got it working, it is close to what Oleg said, just that you do not need to do JSON.stringify.我得到它的工作,它接近奥列格所说的,只是你不需要做 JSON.stringify。

If you have WebMessageBodyStyle.WrappedRequest, this works:如果你有 WebMessageBodyStyle.WrappedRequest,这可行:

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

OR if you have no BodyStyle, this works:或者,如果您没有 BodyStyle,这可行:

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

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

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