SexyMF
SexyMF

Reputation: 11155

WCF wont accept JSON request

I have this code submitted to the server:

var obj = { "param": { "WithdrawalRequestId": window.Xrm.Page.data.entity.getId()} };
    $.ajax({
        type: "POST",
        dataType: "json",   
        data: JSON.stringify(obj),

        url: crmConfig.service + '/WithdrawRequestService.svc/ExecuteWithdrawalRequests/',

    });

the actual content that went to the server is:

{"param":{"WithdrawalRequestId":"{628E2E3A-283A-E311-B658-005056B7032A}"}}

The interface:

 [OperationContract]
        [WebInvoke(Method = "POST",
            RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "/ExecuteWithdrawalRequests/")]
        void ExecuteWithdrawalRequests(GuidParameter param);

The GuidParameter class:

[DataContract]
public class GuidParameter
{
    [DataMember]
    public Guid WithdrawalRequestId { get; set; }

}

I am getting the following error:

The server encountered an error processing the request. 

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.

And this is the chrome output:

`Request URL:http://crm3:81/WithdrawRequestService.svc/ExecuteWithdrawalRequests/
Request Method:POST
Status Code:500 General Error. Please contact support team.
Request Headersview source
Accept:application/json, text/javascript, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:74
Content-Type:application/x-www-form-urlencoded
Host:crm3:81
Origin:http://crm3
Pragma:no-cache
Referer:http://crm3/CRN/userdefined/edit.aspx?_gridType=10019&etc=10019&id=%7b628E2E3A-283A-E311-B658-005056B7032A%7d&pagemode=iframe&rskey=292691624
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Form Dataview sourceview URL encoded
{"param":{"WithdrawalRequestId":"{628E2E3A-283A-E311-B658-005056B7032A}"}}:
Response Headersview source
Access-Control-Allow-Headers:Content-Type, Accept
Access-Control-Allow-Methods:POST
Access-Control-Allow-Origin:*
Cache-Control:private
Content-Length:2738
Content-Type:text/html
Date:Mon, 28 Oct 2013 07:35:33 GMT
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET`

Upvotes: 0

Views: 4359

Answers (2)

Jignesh.Raj
Jignesh.Raj

Reputation: 5987

if you are beginner then this will guide you create json and xml enabled web service which can be consumed by :

Create RESTful WCF Service API: Step By Step Guide

Update:

WCF "Raw" programming model (Web) - receiving arbitrary data

or

Create and Consume JSON-Formatted OData

Upvotes: 0

Nirmal
Nirmal

Reputation: 934

Add contentType: "application/json,

var obj = { "param": { "WithdrawalRequestId": window.Xrm.Page.data.entity.getId()} };
    $.ajax({
        type: "POST",
        dataType: "json",   
        data: JSON.stringify(obj),
        contentType: "application/json",
        url: crmConfig.service + '/WithdrawRequestService.svc/ExecuteWithdrawalRequests/',

    });

Upvotes: 3

Related Questions