WCF不接受JSON请求

时间:2013-10-28 08:03:54

标签: jquery json wcf

我已将此代码提交给服务器:

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/',

    });

转到服务器的实际内容是:

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

界面:

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

GuidParameter类:

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

}

我收到以下错误:

The server encountered an error processing the request. 

异常消息是'传入消息具有意外的消息格式'Raw'  操作的预期消息格式是'Xml','Json'。这可能是因为尚未在绑定上配置WebContentTypeMapper 有关更多详细信息,请参阅WebContentTypeMapper的文档。

这是chrome输出:

`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`

2 个答案:

答案 0 :(得分:3)

添加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/',

    });

答案 1 :(得分:0)

如果您是初学者,那么这将指导您创建可以通过以下方式使用的json和xml启用的Web服务:

Create RESTful WCF Service API: Step By Step Guide

<强>更新

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

Create and Consume JSON-Formatted OData