简体   繁体   中英

Hosted WCF POST Service not working

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM