First of all, it is not recommended to use POST if your Service is just returning data. Use GET instead. But still, if you are going to use POST, then here is the proper method to use it in WCF.
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle=WebMessageBodyStyle.Bare,
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json,
UriTemplate = "/GetData")]
string GetData(MyValues values);
And here is your MyValues
class.
[DataContract]
public class MyValues
{
[DataMember]
public string value1{get; set;}
[DataMember]
public string value2{get; set;}
public override string ToString()
{
JavaScriptSerializer js = new JavaScriptSerializer(); // Available in System.Web.Script.Serialization;
return js.Serialize(this);
}
}
Note that I have written, ToString()
method in MyValues
class. This is because you can get the format of JSON that you will send from JSON call. More details here.
When calling from AJAX, you need to specify charset
as well.
contentType: "application/json; charset=utf-8",
Check now! Make sure that you are sending right JSON in your request. ToString()
method will return you the required JSON format that your service will accept.