Reputation: 21
I am attempting to call a wcf rest service via ajax construct using jquery, however i am getting a bad request error when doing so from the jquery. Also, I try to browse directly to the service and receive a blank page. I have done WCF service calls before in the past and cannot figure out what is wrong here. Thanks in advance to all that reply. When browsing directly to the service I see no results. Here is the jquery ajax code that makes the call:
$.ajax({ type: "GET", dataType: "json", contentType: "application/json; charset=utf-8", url: "http://localhost:57452/mobile/WCFService/ContactService.svc/hello", success: function (result) { alert('success'); }, error: function (result) { alert(result.status + ' ' + result.statusText); }
});
Here is the service interface:
[ServiceContract]
public interface IContactService
{
[OperationContract]
[WebGet(
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "hello")]
string SaySomething();
}
Here is the service class:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ContactService : IContactService
{
public string SaySomething()
{
// Add your operation implementation here
return "Hello!";
}
}
Here is the configuration for the service in the web.config file:
<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>
Upvotes: 1
Views: 1318
Reputation: 21
I found the issue. It appears that when adding a wcf service, the default bindings added to the config file did not add the webHttpBinding. Found the solution from this link, http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery
Upvotes: 1