简体   繁体   English

托管的WCF POST服务不起作用

[英]Hosted WCF POST Service not working

IService 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+ 我已经将上述代码(原型)用于wcf POST服务,并托管在IIS 7.0+

I am unable to call this service using ajaxcallback. 我无法使用ajaxcallback调用此服务。 I have tried the same CODE with GET method, and Hosted on IIS. 我曾尝试使用GET方法使用相同的CODE,并托管在IIS上。 It works perfectly fine. 它工作得很好。 Where I am going wrong with POST method? POST方法哪里出问题了?

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. 第一个是使用WebMessageBodyStyle.WrappedRequest并尝试像裸请求一样传递值。 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. 第二个问题是您发布数据的JSON代码不正确。 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. 以下链接概述了Bare请求和Wrapped请求之间的区别。 http://www.wcf.dotnetarchives.com/2013/12/difference-between-webmessagebodystylew.html http://www.wcf.dotnetarchives.com/2013/12/difference-between-webmessagebodystylew.html

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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