【发布时间】:2014-09-27 03:50:26
【问题描述】:
我正在绑定到 JSON 数据源,然后在用户根据页面上的过滤器启动搜索后重新绑定。 JSON 有效负载编码不正确,到目前为止我尝试过的任何方法似乎都无法解释原因。
如果我可以将正确的 JSON 添加到 HTTP 帖子中,那么一切都会正常工作,并且与首先列出的 $.ajax 方法一样。
使用 $.ajax 调用(有效)
$.ajax(
{
url: '/api/DataProcessing',
type: "Post",
contentType: "application/json; charset=utf-8",
data: '' + JSON.stringify(searchObject),
dataType: 'json',
success: function (result) {
$(".kendoDataProcessing").data("kendoGrid").dataSource = new kendo.data.DataSource({ data: result });
$(".kendoDataProcessing").data("kendoGrid").dataSource.read();
$(".kendoDataProcessing").data("kendoGrid").refresh();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Status: ' + xhr.status + ', Error Thrown: ' + thrownError);
}
});
但是,当我更新 kendogrid 数据源时,我希望发送等效的有效负载,它以一种意想不到的方式对 JSON 进行编码(请参阅下面的代码块,了解在 Fiddler 中捕获的 HTTP 请求之前和之后的代码块。(编码不正确)
$(".kendoDataProcessing").kendoGrid({
dataSource: {
transport: {
read: {
url: '/api/DataProcessing',
type: 'Post',
contentType: 'application/json; charset=utf-8',
data: '' + JSON.stringify(searchObject),
dataType: 'json',
}
},
pageSize: 25
},
height: 620,
sortable: true,
pageable: true,
filterable: true,
columns: [
{
field: "Client",
title: "Client Name",
width: 120
}, {
field: "Study",
title: "Study",
width: 100
}, {
field: "DataLogId",
title: "Batch Description",
width: 120
}, {
field: "Indicator",
title: "Indicator",
width: 100
}, {
field: "UserName",
title: "Username",
width: 110
}, {
field: "AssessmentPoint",
title: "Assessment Point",
width: 130
}, {
field: "DateStamp",
title: "Date Stamp",
width: 180
}]
});
**预期的 JSON 编码(使用 $.ajax 方法创建的 HTTP 调用)**
{"Client":"Choose a client...","Study":"Choose a study...","UserName":"Choose a user...","from":"","To":"","AssessmentPoint":"Choose an AP...","Indicator":"Choose an indicator...","DataLogId":""}
**实际 JSON 编码(使用 Kendogrid 数据源更新和重新绑定创建的 HTTP 调用**
0=%7B&1=%22&2=C&3=l&4=i&5=e&6=n&7=t&8=%22&9=%3A&10=%22&11=C&12=h&13=o&14=o&15=s&16=e&17=+&18=a&19=+&20=c&21=l&22=i&23=e&24=n&25=t&26=.&27=.&28=.&29=%22&30=%2C&31=%22&32=S&33=t&34=u&35=d&36=y&37=%22&38=%3A&39=%22&40=C&41=h&42=o&43=o&44=s&45=e&46=+&47=a&48=+&49=s&50=t&51=u&52=d&53=y&54=.&55=.&56=.&57=%22&58=%2C&59=%22&60=U&61=s&62=e&63=r&64=N&65=a&66=m&67 ... (continues)
看起来它正在将 json 字符串变成一个数组。所以我尝试了一个“floof”的测试字符串,它编码为“0=f&1=l&2=o&3=o&4=f”
调用的控制器方法:
public HttpResponseMessage Post([FromBody]DataProcessingSearch dataProcessingSearch)
{
// dataProcessingSearch var is null (was passed oddly encoded)
}
其他详细信息(搜索对象)
var searchObject = new Object();
searchObject.Client = $('#ClientList').val();
searchObject.Study = $('#StudyList').val();
searchObject.Site = $('#SiteList').val();
searchObject.UserName = $('#UserList').val();
searchObject.from = $('#beginSearch').val();
searchObject.To = $('#endSearch').val();
searchObject.AssessmentPoint = $('#AssessmentPointList').val();
searchObject.Indicator = $('#IndicatorList').val();
searchObject.DataLogId = $('#DataLogIdText').val();
【问题讨论】:
-
查看我的回复,我还提供了一个工作演示,您可以查看源代码..希望对您有所帮助。 PS:我没有使用 ASP 但这个问题与服务器端无关。
标签: ajax asp.net-mvc json encoding kendo-grid