簡體   English   中英

當WCF服務和jquery函數都在不同項目中但在同一解決方案中時,使用jquery調用WCF服務函數

[英]Call a WCF service function using jquery when both the WCF service and jquery function is in different projects but in the same solution

我想使用單獨項目中的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

任何幫助表示贊賞。

不同的端口可能就像不同的域(我不知道),因此也許您必須使用jsonp而不是json。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM