如何解决在调用启用了AJAX的WCF服务之前,修改jqGrid的postData
|| 我有一个具有以下签名的启用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() },
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。