Ajax调用WCF返回"传入的消息具有意外的消息格式' Raw'。操作的预期消息格式。"

时间:2014-04-24 12:35:56

标签: c# ajax json wcf

使用.NET framework 3.5。 我无法想象发生了什么?使用相同的方法,我的Get WCF服务似乎很好。 不确定,缺少什么?

WCF:

[OperationContract]
        [WebInvoke(Method = "POST",
         ResponseFormat = WebMessageFormat.Json,
         BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public void PutInventory(int caseId, DateTime dateReceived, string tamisCaseNo, string oarNo, string tin, string taxPdr, int oarTypeCd, string idrsOrgAssigned, string idrsTeAssigned, DateTime dateRequestComp, int tasCriteriaCd, string tasExpidateCd, DateTime dateEntered, string remarks)
        {
            InventoryDAL.PutInventory(caseId, dateReceived, tamisCaseNo, oarNo, tin, taxPdr, oarTypeCd, idrsOrgAssigned, idrsTeAssigned, dateRequestComp, tasCriteriaCd, tasExpidateCd, dateEntered, remarks);
        }

Ajax调用我的webform:

 $.ajax({
                url: "Services/IVOOARInventoryService.svc/PutInventory",
                type: 'POST',
                cache: false,
                dataType: 'json',
                data: ({
                    caseId: '<%=Request.QueryString["id"]%>', dateReceived: $("#dateEntered").val(), tamisCaseNo: $("#tamisCaseNo").val(), oarNo:  $("#OARNo").val(), tin:$("#tin").val(), taxPdr: $("#taxPeriod").val(), oarTypeCd: $("#oarType").val(), idrsOrgAssigned: $("#idrsOrgAssigned").val(), idrsTeAssigned: $("#idrsTeAssigned").val(), dateRequestComp: $("#dateRequestComp").val(), tasCriteriaCd: $("#tasCriteriaComp").val(), tasExpidateCd:$("#tasExpediateCd").val(), dateEntered: $("#dateEntered").val(), remarks: $("#remarks").val()
                }),
                error: function (jqXHR, textStatus, errorThrown) {
                    $("div#spinner").fadeOut("slow");
                    alert(errorThrown);
                },
                success: function (json) {
                    $("div#spinner").fadeOut("slow");
                }
            });

我的错误:

{"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message":"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.

2 个答案:

答案 0 :(得分:4)

想出来,需要使用“JSON.stringify”

示例:

 data: JSON.stringify({
                    caseId: "18"
                    ...etc..
                }),

http://encosia.com/asmx-scriptservice-mistake-invalid-json-primitive/

答案 1 :(得分:3)

将内容类型设置为application / json

  $.ajax({
            url: "Services/IVOOARInventoryService.svc/PutInventory",
            type: 'POST',
            cache: false,
            dataType: 'json',
            contentType:"application/json",
            data: ({
                caseId: '<%=Request.QueryString["id"]%>', dateReceived: $("#dateEntered").val(), tamisCaseNo: $("#tamisCaseNo").val(), oarNo:  $("#OARNo").val(), tin:$("#tin").val(), taxPdr: $("#taxPeriod").val(), oarTypeCd: $("#oarType").val(), idrsOrgAssigned: $("#idrsOrgAssigned").val(), idrsTeAssigned: $("#idrsTeAssigned").val(), dateRequestComp: $("#dateRequestComp").val(), tasCriteriaCd: $("#tasCriteriaComp").val(), tasExpidateCd:$("#tasExpediateCd").val(), dateEntered: $("#dateEntered").val(), remarks: $("#remarks").val()
            }),
            error: function (jqXHR, textStatus, errorThrown) {
                $("div#spinner").fadeOut("slow");
                alert(errorThrown);
            },
            success: function (json) {
                $("div#spinner").fadeOut("slow");
            }
        });