麻烦与ajax POST调用WCF服务

wot*_*ame 5 asp.net ajax wcf

我从ajax调用WCF服务,我可以让它作为GET请求而不是POST请求.所以:

    [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;
    }
Run Code Online (Sandbox Code Playgroud)

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;
    }
});
Run Code Online (Sandbox Code Playgroud)

但有效:

    [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;
    }
Run Code Online (Sandbox Code Playgroud)

$.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;
    }
});
Run Code Online (Sandbox Code Playgroud)

没有.我错过了一些简单的事吗?我在这里撕扯我的头发.谢谢

Afs*_* Gh 1

我有类似的示例项目。我把它贴在这里,希望它有帮助

网页配置:

<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>
Run Code Online (Sandbox Code Playgroud)

世界碳基金:

<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
Run Code Online (Sandbox Code Playgroud)

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)
            };
Run Code Online (Sandbox Code Playgroud)