开发者

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

I have an AJAX-enabled WCF service with the following signature:

       [OperationContract]
       [WebGet]
    开发者_如何学Python   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.

I've read how its supposed to function and from what I can tell this should work:

 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:

  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:

1) The json params are correct : {"entityIndex":6}

2) The AJAX service returns JSON data to the grid, its just the wrong data

And here is the wierd part:

I logged the 'entityIndex' thats actually working inside the WCF operation -- and its ALWAYS coming up as 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)


I got it working, it is close to what Oleg said, just that you do not need to do JSON.stringify.

If you have WebMessageBodyStyle.WrappedRequest, this works:

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

OR if you have no BodyStyle, this works:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜