Javascript 调用WCF方法

Javascript 调用WCF方法,javascript,jquery,wcf,Javascript,Jquery,Wcf,我有WCF服务 ITourService.cs namespace Service { [ServiceContract] public interface ITourService { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml)]

我有WCF服务

ITourService.cs

namespace Service
{
    [ServiceContract]
    public interface ITourService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml)]
        double?[] GetPoints(string tourname);
    }
}
Web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="TourService.TourService">
        <endpoint binding="webHttpBinding" contract="TourService.ITourService" behaviorConfiguration="webHttp"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>
但是出了点问题

我收到一条消息:服务调用失败:0错误

WCF服务运行良好,我在C上使用standart客户端进行了检查#

我认为从javascript访问WCF是一个错误,但我不知道在哪里

varData = '{"tourname": "customname"}';
应该行。

我想

varData = {tourname: "customname"};

应该工作

要将
varData
对象转换为编码字符串,需要
processData
为true

varData = { tourname : 'customname' }
...
varProcessData = true; 

您已经根据W3C标准实现了一个web服务,即您的web服务需要XML格式的请求和XML格式的响应。但是,在Javascript代码中,可以使用JSON数据创建REST请求

当您监视浏览器和服务之间的通信时,您可能会看到服务的应答是“404未找到”,因为它只在URL上应答,而不在URL上应答

因此,要继续前进,您需要将web服务转变为REST服务。我还建议不仅在请求中使用JSON,在响应中也使用JSON


您可以在这里找到一个使用WCF构建的简单REST服务的示例。

您尝试过吗
varData={'tourname':'customname'}
我添加了ServiceFailed方法,因此我得到了“0错误”消息您现在已经对许多答案发表了评论,您的问题使用了“不起作用”。你能说得更准确些吗?会发生什么?你收到错误信息了吗?你在哪里买的?上面说什么?你能用Firebug或Fiddler记录网络流量吗:你看到了什么?请看我的帖子:我有一条消息:服务呼叫失败:0错误我想我需要在这里进行跨域呼叫。我已经为此创建了一篇帖子:我设置了
varProcessData=true
,但它不适用于
varData={tourname:'customname'}
varData={“tourname”:“customname”}
varData={“tourname”:“customname”}
varData = { tourname : 'customname' }
...
varProcessData = true;