2011-03-15 24 views
1

我在使用wcf服務時遇到問題。在ajax中的響應將會出現錯誤函數。該網址是完全正確的。在jquery中使用wcf服務

$.ajax({ 
      type: "post", 
      url: "http://192.168.1.3:16150/ajaxnewclinicalhost/Service.svc/gettablejson1", 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { 
       alert(msg); 
      }, 
      error: function() { 
       alert("error"); 
      } 
      }); 

的Web.config

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.web> 
     <compilation debug="false" targetFramework="4.0"> 
      <assemblies> 
       <add assembly="System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /></assemblies></compilation> 
    </system.web> 
    <system.serviceModel> 
     <services> 
    <service name="newclinicallibrary.Service1"> 
    <endpoint address="" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="WEB" contract="newclinicallibrary.IService1" /> 
    </service> 
    </services> 
    <behaviors> 
      <serviceBehaviors> 
       <behavior> 
        <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
        <serviceMetadata httpGetEnabled="true" /> 
        <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
        <serviceDebug includeExceptionDetailInFaults="false" /> 
       </behavior> 
      </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="WEB"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
     </behaviors> 
     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
     <modules runAllManagedModulesForAllRequests="true" /> 
     <directoryBrowse enabled="true" /> 
    </system.webServer> 
</configuration> 
+1

瀏覽器中的URL是否具有匹配的IP和端口? – alex 2011-03-15 11:38:15

+0

請向我們展示您的服務器端配置('web.config'和'service.svc'文件)!沒有它,我們不可能知道什麼可能會出錯.... – 2011-03-15 11:40:49

+0

我已經添加web.config請檢查 – user601367 2011-03-15 12:06:00

回答

1

每當使AJAX調用,你應該記住,出於安全考慮,瀏覽器將只允許請求到URL上做出相同的域中頁面本身承擔。所以除非您的網頁是從http://192.168.1.3提供的,否則您將無法對您的WCF服務進行AJAX調用。

如果這變成是的話,你可以嘗試以下操作之一:

  • 主機您在您的網站的一個子域WCF(可能仍從安全問題的影響,由於不同港口)。
  • 在您的網站上編寫一箇中間腳本,代理向您的WCF服務請求。
  • 不要使用WCF - 而是將您的功能置於您網站的腳本中。如果您使用的是ASP.NET,那麼ASHX或Web模塊將是合適的。
+0

我們在同一個域中託管了服務和頁面,但是,仍然是ajax響應去錯誤函數。請幫助它的緊急。 – user601367 2011-03-15 12:58:31