Reputation: 221
IService
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest
)]
string SampleMethod(string UserID, string SID, string TypeID);
Application
$.ajax({
url: serviceurl,
data: '{UserID: 12345, SID: 23123 ,TypeID: 123123}',
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
$.each(JSON.parse(data.d), function (id, obj) {
alert(obj.Msg);
});
}
});
I have used above code(prototype) for wcf POST service and hosted on IIS 7.0+
I am unable to call this service using ajaxcallback. I have tried the same CODE with GET method, and Hosted on IIS. It works perfectly fine. Where I am going wrong with POST method?
Upvotes: 1
Views: 217
Reputation: 2818
EDIT: Adding more information about Wrapped and Bare Requests.
There are two issues in your code. The first one is that you use WebMessageBodyStyle.WrappedRequest and try to pass the values like a Bare Request. You can change the Message body style as follows.
BodyStyle = WebMessageBodyStyle.Bare
Second issue is that your JSON code that is posting the data is not correct. It should be like below. (Note the quotes for the keys)
data: '{"UserID": 12345, "SID":23123,"TypeID":123123}'
The following link outlines the differences between a Bare and a Wrapped request. http://www.wcf.dotnetarchives.com/2013/12/difference-between-webmessagebodystylew.html
Hope this helps.
Upvotes: 1