我有一个启用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 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 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操作中工作的"entityIndex",它总是显示为0?
谢谢。
我不会批评你的程序风格。我可以写太多关于这个的东西。:-)
您当前的主要问题可以通过使用JSON.stringify(pdata.entityIndex())
而不是JSON.stringify(params)
来解决,或者使用WFC方法的另一个BodyStyle
来解决(有关详细信息,请参阅此处)
我让它工作了,它接近于Oleg所说的,只是你不需要做JSON.stringify.
如果您有WebMessageBodyStyle。WrappedRequest,这是有效的:
data: { entityIndex: pdata.entityIndex() },
或者,如果你没有BodyStyle,这是有效的:
data: { "entityIndex": pdata.entityIndex() },