我在从 jQuery DataTables.Net ajax POST 调用到我的 WCF 服务 (4.0) 服务中获取有效 json 时遇到问题。我尝试了很多组合,但无法将这些aoData
值传递给我的服务。
[Apols 有这么多代码……我想我最好把我能想到的都贴上去!]
这是来自 IE F12 调试的调用:
Key Value
Request POST /CustomerService.svc/json/GetCustomerDataTable HTTP/1.1
Accept application/json, text/javascript, */*; q=0.01
Content-Type application/json; charset=utf-8
Referer http://localhost:51901/Home/ListCustomers
Accept-Language en-gb
Accept-Encoding gzip, deflate
User-Agent Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Host localhost:51900
Content-Length 1651
Connection Keep-Alive
Cache-Control no-cache
和请求正文
"{ jsonAOData : [{"name":"sEcho","value":1},{"name":"iColumns","value":7},{"name":"sColumns","value":""},{"name":"iDisplayStart","value":0},{"name":"iDisplayLength","value":10},{"name":"mDataProp_0","value":"CustomerNumber"},{"name":"mDataProp_1","value":"FirstName"},{"name":"mDataProp_2","value":"FamilyName"},{"name":"mDataProp_3","value":"MobileNumber"},{"name":"mDataProp_4","value":"CustomerIdNumber"},{"name":"mDataProp_5","value":"CustomerIdType"},{"name":"mDataProp_6","value":"DateOfBirth"},{"name":"sSearch","value":""},{"name":"bRegex","value":false},{"name":"sSearch_0","value":""},{"name":"bRegex_0","value":false},{"name":"bSearchable_0","value":true},{"name":"sSearch_1","value":""},{"name":"bRegex_1","value":false},{"name":"bSearchable_1","value":true},{"name":"sSearch_2","value":""},{"name":"bRegex_2","value":false},{"name":"bSearchable_2","value":true},{"name":"sSearch_3","value":""},{"name":"bRegex_3","value":false},{"name":"bSearchable_3","value":true},{"name":"sSearch_4","value":""},{"name":"bRegex_4","value":false},{"name":"bSearchable_4","value":true},{"name":"sSearch_5","value":""},{"name":"bRegex_5","value":false},{"name":"bSearchable_5","value":false},{"name":"sSearch_6","value":""},{"name":"bRegex_6","value":false},{"name":"bSearchable_6","value":false},{"name":"iSortCol_0","value":0},{"name":"sSortDir_0","value":"asc"},{"name":"iSortingCols","value":1},{"name":"bSortable_0","value":true},{"name":"bSortable_1","value":true},{"name":"bSortable_2","value":true},{"name":"bSortable_3","value":true},{"name":"bSortable_4","value":true},{"name":"bSortable_5","value":false},{"name":"bSortable_6","value":false}]}"
这给出了错误:
There was an error deserializing the object of type System.String. The token 'null' was expected but found 'name'.
我尝试了各种构建方式,data: '"{ jsonAOData : ' + jsonAOData + '}"',
但还没有找到任何有效的组合。
我已经浏览了很多关于 SO 的东西,但还没有找到解决方案。
注意:
如果我发送:
data: '{ "jsonAOData" : "' + 'BOBBLE' + '"}',
然后我得到了错误:
There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'jsonAOData' from namespace ''.
TBH:我不知道这意味着什么!这是一个字符串,而不是 XML,所以它为什么要寻找“根”——此外,我想要 jsonAOData,不是吗?
注意
如果我用相同的调用(只是不同的 URI)调用我的 MVC3 控制器,那么它会按预期工作并且我的 jsonAOData 字符串是否正确传递?
代码
[ServiceContract]
public interface ICustomerService
{
[OperationContract]
[WebInvoke(Method="POST",
BodyStyle= WebMessageBodyStyle.Bare,
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json,
UriTemplate = "GetCustomerDataTable")]
string GetCustomerDataTable(string jsonAOData);
}
-
public string GetCustomerDataTable(string jsonAOData)
{
var log = LogManager.GetLogger
//Code omitted for brevity...
var result = serializer.Serialize(jsonDataTable);
return result;
}
-
$(document).ready(function () {
jQuery.support.cors = true; // cross site
var dt = $('#dataTable').dataTable({
"bProcessing": true,
"bSort": true,
"bServerSide": true,
//"sAjaxSource": "JsonSearch", //Call to MVC controller...works as expected
"sAjaxSource": "http://localhost:51900/CustomerService.svc/json/GetCustomerDataTable",
"sServerMethod": "POST",
"fnServerData": function (sSource, aoData, fnCallback) {
var jsonAOData = JSON.stringify(aoData);
//alert(jsonAOData);
$.ajax({
dataType: 'json',
crossDomain: true,
contentType: "application/json; charset=utf-8",
type: "POST",
url: sSource,
data: '"{ jsonAOData : ' + jsonAOData + '}"', //I have tried many combinations of this!
dataType: "json",
success: function (data) {
//alert('success');
fnCallback($.parseJSON(data));
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + XMLHttpRequest.status + "\r\n" + textStatus + "\r\n" + errorThrown);
}
});
},
"aoColumnDefs": [
{ "aTargets": [0], "sTitle": "Customer Number", "mData": "CustomerNumber", "bSearchable": true, "bSortable": true },
{ "aTargets": [1], "sTitle": "First Name", "mData": "FirstName", "bSearchable": true, "bSortable": true },
{ "aTargets": [2], "sTitle": "Family Name", "mData": "FamilyName", "bSearchable": true, "bSortable": true },
{ "aTargets": [3], "sTitle": "Mobile Number", "mData": "MobileNumber", "bSearchable": true, "bSortable": true },
{ "aTargets": [4], "sTitle": "Customer Id", "mData": "CustomerIdNumber", "bSearchable": true, "bSortable": true },
{ "aTargets": [5], "sTitle": "Id Type", "mData": "CustomerIdType", "bSearchable": false, "bSortable": false },
{ "aTargets": [6], "sTitle": "Date Of Birth", "mData": "DateOfBirth", "bSearchable": false, "bSortable": false },
],
"sScrollY": "500",
"bScrollCollapse": true
});
dt.fnSetFilteringDelay(1000);
});
-
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="jsonCustomerServiceBinding" maxBufferSize="26214400"
maxReceivedMessageSize="26214400" />
</webHttpBinding>
</bindings>
<services>
<service name="Ibq.WcfService.CustomerService">
<endpoint address="json" behaviorConfiguration="json" binding="webHttpBinding"
bindingConfiguration="jsonCustomerServiceBinding" name="jsonCustomerService"
contract="Ibq.WcfService.ICustomerService" kind="webHttpEndpoint"
endpointConfiguration="" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:51900/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="json">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"
faultExceptionEnabled="false"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
</system.serviceModel>