簡體   English   中英

對WCF的Ajax調用返回錯誤

[英]Ajax call to WCF returning error

我收到錯誤消息:

異常消息為“傳入消息的消息格式為意外的原始”。 該操作的預期消息格式為“ 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();
}

我在通話中做錯了什么嗎?

  1. 您的m_Data似乎有一個錯誤。 兩個項目之間沒有逗號(m_Data =“ {'fromdate':'” + getFromDate +“ 'getvaluelist':'[1,2,3]'}”;)
  2. 匹配參數名稱( fromdate > getFromDategetvaluelist > 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] }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM