2014-03-07 26 views
0

代码WCF服务在HTML页面中没有得到respose

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script> 
    $(document).ready(function() { 
     $.ajax({ 
      url: "http://localhost:3535/WCFService1/Service.svc/rest/hello/Govinda", 
      type: "GET", 
      success: function (result) { 
       alert($(result).find("string").text()); 
      }, 
      error: function (error) { 
       alert("hi" + error); 
      } 
     }); 
    }); 
</script> 

我使用wcf service未对服务器承载我只是测试它在本地server.When我在.aspx页面中使用上面的代码,然后它工作正常。但是,当我在.html文件上运行此代码时,它不起作用。这两个文件都在我的项目中。所以,如果有人对这个问题有任何想法,那么请帮助我或建议我在哪里做错了。

+0

你得到任何错误? – Tan

+0

不,我没有得到任何错误。 –

+0

即使在网页浏览器控制台中也没有?然后它很奇怪,因为你说它在aspx页面中工作,但不在html页面中。你没有使用任何.net/aspx功能组件,它只是JavaScript,所以我认为它必须是错误的JavaScript或在.html页面加载jQuery。 – Tan

回答

0

试试这个方法:

IService.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together. 
[ServiceContract] 
public interface IService 
{ 

    [OperationContract] 
    [WebGet (UriTemplate="getData/{value}",ResponseFormat=WebMessageFormat.Json)] 
    string GetData(string value); 
} 

Service.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together. 
public class Service : IService 
{ 
    DataClassesDataContext de = new DataClassesDataContext(); 
    public string GetData(string value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 
} 

Web。配置:

<?xml version="1.0"?> 
<configuration> 
    <connectionStrings> 
    <add name="DatabaseConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5"> 
     <assemblies> 
     <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 
     </assemblies> 
    </compilation> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="Service" behaviorConfiguration="ServBehave"> 
     <!--Endpoint for REST--> 
     <endpoint address="rest" binding="webHttpBinding" behaviorConfiguration="restPoxBehavior" contract="IService" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServBehave"> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <!--Behavior for the REST endpoint for Help enability--> 
     <behavior name="restPoxBehavior"> 
      <webHttp helpEnabled="true" /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <httpProtocol> 
    <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
    </customHeaders> 
    </httpProtocol> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 
</configuration> 

HTML:

<html> 
    <head> 
     <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
     <script> 
      $(document).ready(function(){ 
       $.ajax({ 
        url: "http://localhost:4102/Service.svc/rest/getData/Chintan", 
        type: "GET", 
        success: function(result) 
        { 
         alert(result); 
        }, 
        error: function(error) 
        { 
         alert("error"); 
        } 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
    </body> 
</html> 

我使用Visual Studio 2013.net framework 4.5

+0

我使用visual studio 2010 4.0 framework –

+0

感谢它与2013合作! –

1

试试这个!

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script> 
    $(document).ready(function() { 
     $.ajax({ 
      url: "http://localhost:3535/WCFService1/Service.svc/rest/hello/Govinda", 
      type: "GET", 
      dataType: 'jsonp', 
      crossDomain: true, 
      success: function (result) { 
       alert($(result).find("string").text()); 
      }, 
      error: function (error) { 
       alert("hi" + error); 
      } 
     }); 
    }); 
</script> 

编辑在您的WCF服务中启用Cors。 http://enable-cors.org/server_wcf.html

+0

也许你不需要crossDomain:true只有jsonp – Tan

+1

我可以解决这个问题,当我从'.html'文件调用服务时,我在我的项目中调用服务时得到响应,但是当我从'调用此URL时。 HTML页面是项目之外,那么我没有得到回应。 –

+0

@GovindaRajbhar是否添加了dataType:'jsonp'? – Tan

1

由于同源策略,似乎您的浏览器阻止了该请求。尝试使用url的相对路径而不是绝对路径。由于你的html文件在同一个项目中。

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script> 
$(document).ready(function() { 
    $.ajax({ 
     url: "/WCFService1/Service.svc/rest/hello/Govinda", 
     type: "GET", 
     success: function (result) { 
      alert($(result).find("string").text()); 
     }, 
     error: function (error) { 
      alert("hi" + error); 
     } 
    }); 
}); 

+0

我相信这会起作用,但是当wcf服务器不在同一台机器上时,这将不起作用。因为他说他正在本地进行测试。 – Tan

+0

@Tan如果他使用jsonp,他应该将响应包装到服务器中的一个函数中 –

+1

我可以解决这个问题,并且当我从我的项目中的'.html'文件调用服务时得到响应。但是当我从项目之外的'.html'页面调用这个URL,然后我没有得到响应。 –

1

你没有得到从不同势机URL任何回应的原因是怎么一回事,因为您的Web服务器没有配置与Cross domain (CORS)工作。

为了使它工作,你需要enable CORS您的Web服务器上

Enabling CORS是添加以下内容到网站的web.config简单,

<system.webServer> 
    <httpProtocol> 
    <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
    </customHeaders> 
    </httpProtocol> 
</system.webServer> 

有关详细信息,了解CORS如何工作访问之后链接:

http://encosia.com/using-cors-to-access-asp-net-services-across-domains/

http://blogs.msdn.com/b/carlosfigueira/archive/2012/05/15/implementing-cors-support-in-wcf.aspx?Redirected=true

http://enable-cors.org/server_wcf.html

http://dhvik.blogspot.in/2011/06/supporting-cross-origin-resource.html