2012-08-15 79 views
0

,我發現了錯誤:Ajax調用WCF返回錯誤

The exception message is 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details.

我正在做的AJAX調用WCF服務,像這樣:

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
  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] 
}); 
+0

我改變M_DATA varible到 M_DATA = JSON.stringify({ getFromDate: 「 '」 + getFromDate + 「'」, getValueList:[1,2,3] }); – CodeMan5000 2012-08-15 17:58:58

+0

我現在正在收到錯誤的請求錯誤 – CodeMan5000 2012-08-15 17:59:16