1

我正在尝试使用 jquery 通过 ajax 构造调用 wcf 休息服务,但是从 jquery 执行此操作时出现错误的请求错误。此外,我尝试直接浏览到该服务并收到一个空白页面。我过去曾进行过 WCF 服务调用,但无法弄清楚这里出了什么问题。提前感谢所有回复。直接浏览服务时,我看不到任何结果。这是进行调用的 jquery ajax 代码:

$.ajax({ type: "GET", dataType: "json", contentType: "application/json; charset=utf-8", url: "http://localhost:57452/mobile/WCFService/ContactService.svc/你好”,成功:函数(结果){警报('成功');},错误:函数(结果){警报(result.status +''+ result.statusText);}

    });

这是服务接口:

[ServiceContract]
    public interface IContactService
    {
        [OperationContract]
        [WebGet(
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "hello")]
        string SaySomething();

    }

这是服务类:

 [AspNetCompatibilityRequirements(RequirementsMode =      AspNetCompatibilityRequirementsMode.Allowed)]
    public class ContactService : IContactService
    {
        public string SaySomething()
        {
            // Add your operation implementation here
            return "Hello!";
        }
    }

以下是 web.config 文件中服务的配置:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <behaviors>
      <serviceBehaviors>
        <behavior name="SomeNameSpace.mobile.WCFService.ContactServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="LeeCounty_ASP.mobile.WCFService.ContactServiceBehavior"
        name="SomeNameSpace.mobile.WCFService.ContactService">
        <endpoint address="" binding="wsHttpBinding" contract="SomeNameSpace.mobile.WCFService.IContactService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
4

1 回答 1

1

我发现了这个问题。看来添加wcf服务时,配置文件中添加的默认绑定并没有添加webHttpBinding。从这个链接找到解决方案,http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery

于 2012-05-28T00:31:24.913 回答