开发者

Trouble with ajax POST call to WCF service

开发者 https://www.devze.com 2023-02-24 09:46 出处:网络
I am calling a WCF service from ajax and I can get it to work as a GET request but not a POST request. So:

I am calling a WCF service from ajax and I can get it to work as a GET request but not a POST request. So:

    [OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public UserObject GetUser(string name)
    {
        // Add your operation implementation here
        var uo = new UserObject() { CustClass = "Guest", fname = "Chris", Email = "chris@Something", IsMobile = false };
        uo.fname = name;
        return uo;
    }

and

var json = { "name": "test" }; 
$.ajax({ //get user name and customer class
    type: "GET",
    url: "WritingAnalysisService.svc/GetUser",
    data: json,
    processData: true,
    contentType: "application/json",
    timeout: 10000, 
    dataType: "json", 
    cache: false,
    success: function (data) { //get user name and customer class
        customerclass = data.d.custclass;
        ffname = data.d.fname;
    }
});

works but:

    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public UserObject GetUser(string name)
    {
        // Add your operation implementation here
        var uo = new UserObject() { CustClass = "Guest", fname = "Chris", Email = "chris@Something", IsMobile = false };
        uo.fname = name;
        return uo;
    }

and

$.ajax({ //get user name and customer class
    type: "POST",开发者_如何学编程
    url: "WritingAnalysisService.svc/GetUser",
    data: json,
    processData: true,
    contentType: "application/json",
    timeout: 10000, 
    dataType: "json", 
    cache: false,
    success: function (data) { //get user name and customer class
        customerclass = data.d.custclass;
        ffname = data.d.fname;
    }
});

doesn't. Am I missing something simple? I'm tearing my hair out here. Thanks


Use

BodyStyle = WebMessageBodyStyle.WrappedRequest 

for WebInvokeAttribute


I had similar sample project. I post it here, hope it helps:

Web.config:

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="WebFormApp.MyWcfServiceAspNetAjaxBehavior">
                <enableWebScript/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
        <service name="WebFormApp.MyWcfService">
            <endpoint address="" behaviorConfiguration="WebFormApp.MyWcfServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="WebFormApp.MyWcfService"/>
        </service>
    </services>
</system.serviceModel>

WCF:

<ServiceContract(Namespace:="")> _
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class MyWcfService

    <OperationContract(), _
    WebInvoke(Method:="POST", _
              BodyStyle:=WebMessageBodyStyle.WrappedRequest, _
              ResponseFormat:=WebMessageFormat.Json)> _
    Public Function GetTheEntity() As MyEntity
        Return New MyEntity With {.Name = "TheName", .Family = "TheFamily"}
    End Function

End Class

JS:

function doTestJQuery() {
                $.ajax({
                    type: "POST",
                    url: "MyWcfService.svc/GetTheEntity",
                    data: null,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    processdata: true,
                    success: srvSuccessJQuery,
                    error: null
                });
            }

            function srvSuccessJQuery(result) {
                alert(result.d.Family)
            };
0

精彩评论

暂无评论...
验证码 换一张
取 消