2011-11-29 36 views
2

我想單獨項目中使用jquery函數訪問單獨項目中的WCF函數。當WCF服務和jquery函數在不同的項目中,但在同一個解決方案中調用WCF服務函數使用jquery

CODE:

<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(); 

我總是得到未定義的錯誤。

而且使用招我發現錯誤編號爲400

任何幫助表示讚賞。

+0

具有相同問題 –

+1

我不知道這是你唯一的問題,但它顯示你的腳本期待JSON數據。你的服務似乎在使用basicHttpBinding,它不支持JSON。對於JSON,你需要使用webHttpBinding。另外,您是否嘗試過通過WcfTestClient調用您的服務來驗證它們實際上是否正在工作? –

+0

@ chris.house.00我已經使用WcfTestClient進行了驗證,服務沒問題。需要通過更改綁定來檢查 – Moons

回答

0

不同的端口可能會像不同的域(我不知道),所以也許你必須使用jsonp而不是json。

+0

JsonP沒問題。但是你只能將它用作GET請求,而且似乎安全性較低 – Moons

相關問題