0

我收到错误:

异常消息是“传入消息具有意外消息格式“原始”。该操作的预期消息格式为“Xml”、“Json”。这可能是因为尚未在绑定上配置 WebContentTypeMapper。有关详细信息,请参阅 WebContentTypeMapper 的文档。有关更多详细信息,请参阅服务器日志。

我正在像这样对 WCF 服务进行 ajax 调用:

function WCFJSON() {
var now = new Date();

var getFromDate = dateToWcf(new Date(now - (60000 * 1440)));

var userid = "1";
m_Type = "POST";
m_Url = "https://dev-04.boldgroup.int/ManitouDashboard/DashboardProxyService.svc/GetStats"
m_Data = "{'fromdate':'" + getFromDate + "'getvaluelist':'[1,2,3]'}";
m_DataType = "json";
m_ProcessData = true;             
CallService();
}

function dateToWcf(input) {
var d = new Date(input);
if (isNaN(d)) {
    throw new Error("input not date");
}
var date = '\\\/Date(' + d.getTime() + '-0000)\\\/';
return date;
}

function CallService() {
$.ajax({
    type: m_Type,           //GET or POST or PUT or DELETE verb                  
    url: m_Url,                 //Location of the service   
    data: m_Data,
    dataType: m_DataType,   //Expected data format from server                  
    processdata: m_ProcessData, //True or False
    crossdomain: true,    
    contentType: "application/json",             
    success: function (msg) {   //On Successfull service call                      
        ServiceSucceeded(msg);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        ServiceFailed("jqXHT: " + jqXHR.result + "Text Status: " + textStatus + " Error Thrown: " + errorThrown );
    } // When Service call fails              
});
}

我的服务合同声明如下:

[ServiceContract]
public interface IDashboardWCFService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "GetStats", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    Dictionary<int,List<StatValue>> GetStats(DateTime getFromDate, List<int> getValueList);

    [OperationContract]
    [WebGet(UriTemplate = "GetStatTypes", ResponseFormat = WebMessageFormat.Json)]
    List<StatType> GetStatTypes();
}

我在通话中做错了什么吗?

4

1 回答 1

1
  1. 您的m_Data. 两个项目之间没有逗号 (m_Data = "{'fromdate':'" + getFromDate + " , 'getvaluelist':'[1,2,3]'}";)
  2. 匹配参数名称 ( fromdate-> getFromDate, getvaluelist-> getValueList)
  3. 使用 ISO 8601 日期时间格式 (2012-08-15T00:00:00+02:00) (我总是使用XDate作为日期/时间,它踢屁股)
  4. 删除多余的刻度以防万一,并使用JSON.stringify.
m_Data = JSON.stringify({
  getFromDate: "'" + getFromDate + "'",
  getValueList: [1,2,3]
});
于 2012-08-15T16:54:19.130 回答