簡體   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