How can I POST json data to a WCF web service?

2019-09-09 05:39发布

问题:

I have the following method on a wcf web service.

   [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public void UpdateAnalysisParameters(string parameterSets)
    {
       //....
    }

But I am having trouble posting data to it. I am using the following jquery Ajax call.

    $.ajax({
        url: "/ATOMWebService.svc/UpdateAnalysisParameters",
        dataType: "json",
        type: "POST",
        data: JSON.stringify({ parameterSets: "Dave" })
    });

But the web service is responding with "The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'."

How can I post json data to this web service?

回答1:

Try specifying the content type in your ajax request:

contentType: "application/json"

Setting the dataType only gives a hint to jquery about how to handle the received response.



标签: jquery wcf rest