2013-10-28 44 views
0

我已经提交给服务器验证码:WCF不会接受JSON请求

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. 

异常消息是“收到的消息有一个意外的消息格式‘原始’。
该操作的预期消息格式为'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` 
+0

删除 '{' 和 '}' 从 “{628E2E3A-283A-E311-B658-005056B7032A}”,然后尝试发送字符串。 – Nirmal

+0

@Nirmal - 我做了同样的事情。顺便说一句,它是用“”包裹,所以它不会令人不安,无论如何,它不工作。谢谢 – Shazam

回答

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

    }); 
+0

可能你是正确的,如果生病没有找到其他答案,你会得到五,问题我们,我从端口80到81,添加自定义头将导致CORSS行动,浏览器将触发OPTIONS方法类型之前原来的请求,并会在服务器上引发其他错误) - :(http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) – Shazam

+0

然后你应该使用JSONP的跨域 – Nirmal