简体   繁体   中英

Ajax call to WCF returning error

I'm getting the error:

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.

I'm making the ajax call to the WCF Service like so:

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              
});
}

My Service contract is declared like this:

[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();
}

Am I doing something incorrect in the call?

  1. There seems to be an error in your m_Data . There is no comma between two items (m_Data = "{'fromdate':'" + getFromDate + " , 'getvaluelist':'[1,2,3]'}";)
  2. Match parameter names ( fromdate -> getFromDate , getvaluelist -> getValueList )
  3. Use ISO 8601 date time format (2012-08-15T00:00:00+02:00) (i always use XDate for date/time, it kicks ass)
  4. Remove extra ticks just in case, and use JSON.stringify .
 m_Data = JSON.stringify({ getFromDate: "'" + getFromDate + "'", getValueList: [1,2,3] }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM