问题描述
||
我有一个具有以下签名的启用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!\');
}
});
Ive在Firebug中确认了以下内容:
1)json参数正确:{\“ entityIndex \”:6}
2)AJAX服务将JSON数据返回到网格,它只是错误的数据
这是更奇怪的部分:
我记录了\'entityIndex \'那实际上是在WCF操作内部工作的-它的总值为0?
谢谢。
解决方法
我不会批评您的程序风格。我可以为此写很多东西。 :-)
当前的主要问题可以通过使用
JSON.stringify(pdata.entityIndex())
而不是ѭ5another或通过使用其他BodyStyle
的WFC方法来解决(有关详细信息,请参见此处)
, 我得到了它的支持,它与Oleg所说的很接近,只是您不需要执行JSON.stringify。
如果您具有WebMessageBodyStyle.WrappedRequest,则可以这样做:
data: { entityIndex: pdata.entityIndex() },
或者,如果您没有BodyStyle,则可以使用:
data: { \"entityIndex\": pdata.entityIndex() },