我试图使用jquery通过ajax构造调用wcf rest服务,但是当从jquery执行此操作时,我收到了错误的请求错误。此外,我尝试直接浏览该服务并收到一个空白页面。我以前做过WCF服务调用,无法弄清楚这里有什么问题。在此先感谢所有回复。直接浏览服务时,我看不到任何结果。这是调用的jquery ajax代码:
$。AJAX({ 类型:“GET”, dataType:“json”, contentType:“application / json; charset = utf-8”, url:“http:// localhost:57452 / mobile / WCFService / ContactService.svc / hello”, 成功:功能(结果){ 警报( '成功'); }, 错误:function(result){ alert(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>
答案 0 :(得分:1)
我发现了这个问题。在添加wcf服务时,添加到配置文件的默认绑定似乎没有添加webHttpBinding。通过此链接找到解决方案http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery