0

I am sending a simple ajax call with WCF method, whenit is returning bad-request but without method it is showing status to success

Ajax Call

$(document).ready(function(){ $.get("http://localhost:1347/Service1.svc/DoWork", //this shows bad-request //$.get("http://localhost:1347/Service1.svc", //this shows success function(data, status){ alert("Data: " + data + "\nStatus: " + status); }); 

Operation Contract

namespace WcfService1 { [ServiceContract] public interface IService1 { [OperationContract] string DoWork(); } } 

Implementation

public class Service1 : IService1 { public string DoWork() { return string.Format("This is DoWork()"); } } 

Global.asax

protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); HttpContext.Current.Response.End(); } } 

Web.Config (Edit)

<?xml version="1.0"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5.2"/> <httpRuntime targetFramework="4.5.2"/> <httpModules> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/> </httpModules> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https"/> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"> <remove name="ApplicationInsightsWebTracking"/> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler"/> </modules> <!-- To browse web app root directory during debugging, set the value below to true. Set to false before deployment to avoid disclosing web app folder information. --> <directoryBrowse enabled="true"/> <validation validateIntegratedModeConfiguration="false"/> </system.webServer> </configuration> 
9
  • Making a request to Service1.svc is quite different from Service1.svc/DoWork, since the first one will return an information page about the service and the second one will execute a method. Commented Aug 18, 2016 at 6:44
  • how to execute second one? Commented Aug 18, 2016 at 6:44
  • Can you browse to localhost:1347/Service1.svc/DoWork with a browser, eg. Chrome? Commented Aug 18, 2016 at 6:45
  • attach the config here i think the issue is with config Commented Aug 18, 2016 at 6:47
  • showing empty screen in chrome with this error in console Failed to load resource: the server responded with a status of 400 (Bad Request) Commented Aug 18, 2016 at 6:47

1 Answer 1

2

First, make sure that you enable GET in your contract.

namespace WcfService1 { [ServiceContract] public interface IService1 { [OperationContract] [WebGet(UriTemplate = "DoWork", ResponseFormat = WebMessageFormat.Json)] string DoWork(); } } 

And then make sure to add webHttp in your config.

 <endpointBehaviors> <behavior> <webHttp /> </behavior> </endpointBehaviors> 

You also might need to set your binding to webHttpBindig.

I usually define my endpoints, but I think you can manage without it. This should be a complete example.

 <system.serviceModel> <services> <service name="WcfService1.IService1" behaviorConfiguration="myServiceBehavior"> <endpoint address="" binding="webHttpBinding" behaviorConfiguration="myWebBehavior" contract="WcfService1.IService1" /> </service> </services> <behaviors> <endpointBehaviors> <behavior name="myWebBehavior"> <webHttp /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="myServiceBehavior"> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https"/> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> </system.serviceModel> 

A complete example can be found here.

http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, it was wrong. webHttp is an endpointbehavior. I've updated with a complete example.
following the example in link you have provided. I did exactly same, with this link in browser localhost:3700/TestService.svc/json/123 retuned this Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
With the solution provided you should be able to call localhost:1347/Service1.svc/DoWork. But not pass the parameters with trailing slashes.
however this helped me much, almost I can say accepted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.