无法从JavaScript调用Wcf服务
我有一个无法从JavaScript调用Wcf服务,javascript,html,wcf,jquery,Javascript,Html,Wcf,Jquery,我有一个WCF服务,我通过JS使用Jquery调用来使用它 代码如下所示: IService1.cs: [ServiceContract] public interface IService1 { [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessage
WCF
服务,我通过JS
使用Jquery
调用来使用它
代码如下所示:
IService1.cs:
[ServiceContract]
public interface IService1
{
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
User GetUser(string userName, string password);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public User GetUser(string userName, string password)
{
//DO SOMETHING
}
}
<%@ ServiceHost Language="C#" Debug="true" Service="MyProjectName.Service1" CodeBehind="Service1.svc.cs" %>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true"/>
<identity impersonate="false" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="MyProjectName.Service1"
behaviorConfiguration="ServiceBehavior">
<endpoint address=""
behaviorConfiguration="EndpBehavior"
binding="webHttpBinding"
contract="MyProjectName.IService1" />
</service>
</services>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
Service1.cs:
[ServiceContract]
public interface IService1
{
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
User GetUser(string userName, string password);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public User GetUser(string userName, string password)
{
//DO SOMETHING
}
}
<%@ ServiceHost Language="C#" Debug="true" Service="MyProjectName.Service1" CodeBehind="Service1.svc.cs" %>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true"/>
<identity impersonate="false" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="MyProjectName.Service1"
behaviorConfiguration="ServiceBehavior">
<endpoint address=""
behaviorConfiguration="EndpBehavior"
binding="webHttpBinding"
contract="MyProjectName.IService1" />
</service>
</services>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
由IIS完成托管:
[ServiceContract]
public interface IService1
{
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
User GetUser(string userName, string password);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public User GetUser(string userName, string password)
{
//DO SOMETHING
}
}
<%@ ServiceHost Language="C#" Debug="true" Service="MyProjectName.Service1" CodeBehind="Service1.svc.cs" %>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true"/>
<identity impersonate="false" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="MyProjectName.Service1"
behaviorConfiguration="ServiceBehavior">
<endpoint address=""
behaviorConfiguration="EndpBehavior"
binding="webHttpBinding"
contract="MyProjectName.IService1" />
</service>
</services>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
虽然我可以在浏览器中启动服务,但我不断收到错误:
你离线了!!请检查您的网络
我找不到任何东西来帮助我解决这个问题,有人有主意吗?我想你的问题是你的WS不允许跨域访问 您可以尝试将下一个代码添加到Web.config:
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
<!-- Rest of your code -->
</configuration>
我认为您的问题在于WS不允许跨域访问 您可以尝试将下一个代码添加到Web.config:
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
<!-- Rest of your code -->
</configuration>
不知道您是否尝试过使用jsonp进行跨站点脚本编写。
这是我在网上找到的一个例子。
使用JSONP是一种绕过同源策略限制的技术。JSONP有其优点和缺点。花些时间研究其他可能更好地满足项目需求的方法
不知道您是否尝试过使用jsonp进行跨站点脚本编写。
这是我在网上找到的一个例子。
使用JSONP是一种绕过同源策略限制的技术。JSONP有其优点和缺点。花些时间研究其他可能更好地满足项目需求的方法
参考这个参考this@HodayaShalom您使用的IIS版本是什么?@HodayaShalom这里有一个链接,他们在其中解释它@HodayaShalom我在配置中添加了更多的头,请尝试使用所有这些头。在IIS 7.5中,您似乎还需要
,这就是我询问您的版本的原因;)@HodayaShalom是的,我们在服务器中添加了支持,以允许这样做,但我认为您现在的问题在于客户端。让我查一下:)@HodayaShalom嘿,Hodaya,很高兴听到这个消息!,我很高兴你能修好它@HodayaShalom您使用的IIS版本是什么?@HodayaShalom这里有一个链接,他们在其中解释@HodayaShalom我在配置中添加了更多标题,请尝试所有这些标题。在IIS 7.5中,您似乎还需要
,这就是我询问您的版本的原因;)@HodayaShalom是的,我们在服务器中添加了支持,以允许这样做,但我认为您现在的问题在于客户端。让我查一下:)@HodayaShalom嘿,Hodaya,很高兴听到这个消息!,我很高兴你能修好它!如果我将数据类型更改为数据类型:“jsonp”,我就不会使用success函数,也不会使用error函数。@HodayaShalom-嗯,这并不是那么简单。您的服务器需要知道如何处理jsonp请求,并且需要知道如何构建适当的响应。这里()是一个可以用来生成jsonp响应的HttpModule示例。(在本例中,他使用了asmx WS,但我相信您将更改必要的内容)。仍然不理解为什么不能使用JSON。就像现在一样。@HodayaShalom-请阅读关于同源策略和JSONP的内容。这里()是关于JSONP的维基百科解释,以及相关主题的相关链接。通常,来自www.server1.com的网页不能通过脚本调用www.server2.com。此规则有一些例外(如标记)。JSONP是一种绕过此规则的方法。如果我将数据类型更改为数据类型:“JSONP”,我就不会使用成功函数,也不会使用错误函数。@HodayaShalom-嗯,这并不是那么简单。您的服务器需要知道如何处理jsonp请求,并且需要知道如何构建适当的响应。这里()是一个可以用来生成jsonp响应的HttpModule示例。(在本例中,他使用了asmx WS,但我相信您将更改必要的内容)。仍然不理解为什么不能使用JSON。就像现在一样。@HodayaShalom-请阅读关于同源策略和JSONP的内容。这里()是关于JSONP的维基百科解释,以及相关主题的相关链接。通常,来自www.server1.com的网页不能通过脚本调用www.server2.com。此规则有一些例外(如标记)。JSONP是一种绕过此规则的方法。