当WCF服务和jquery函数在不同的项目中,但在相同的解决方案中,使用jquery调用WCF服务函数

本文关键字:函数 WCF jquery 服务 解决方案 使用 调用 项目 | 更新日期: 2023-09-27 18:16:00

我想在单独的项目中使用jquery函数访问WCF函数。

代码:

<script type="text/javascript">
    $.ajax({
        type: "post",
        url: "http://localhost:3241/EntityService.svc/GetGenders",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (result) {
            alert("success..");
            alert(result);
        },
        error: function (a, b, c) { alert(c); },
        failure: function (msg) {
            alert(msg);
        }
    });

</script>

web . config:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IAuthService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
            <binding name="BasicHttpBinding_IEntityService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
            <binding name="BasicHttpBinding_Ijson" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:3241/AuthService.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IAuthService" contract="QHRAuthService.IAuthService"
            name="BasicHttpBinding_IAuthService" />
        <endpoint address="http://localhost:3241/EntityService.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IEntityService" contract="QHREntityService.IEntityService"
            name="BasicHttpBinding_IEntityService" />
        <endpoint address="http://localhost:2214/json.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_Ijson" contract="ServiceReference1.Ijson"
            name="BasicHttpBinding_Ijson" />
    </client>
</system.serviceModel>
WCF服务:

[OperationContract]
gender_obj[] GetGenders();

我总是得到未定义错误。

使用Fiddler,我发现错误数是400

当WCF服务和jquery函数在不同的项目中,但在相同的解决方案中,使用jquery调用WCF服务函数

不同的端口可能像不同的域(我不知道)所以也许你必须使用json而不是json