I am trying to call WCF REST service with POST method.
[OperationContract]
[AllowCrossSiteJson]
[WebInvoke(UriTemplate = "/PerformAction",
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[FaultContract(typeof(CpiFaultContract))]
string PerformAction(ActionMetaData data);
If I use the following C# code I am able to call the service correctly:
var serializer = new JavaScriptSerializer();
var jsonRequestString = serializer.Serialize(credentail);
var bytes = Encoding.UTF8.GetBytes(jsonRequestString);
// Initiate the HttpWebRequest with session support with CookiedFactory
var request = CreateHttpWebRequest("http://aviary.cloudapp.net/PerformAction");
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
// Send the json data to the Rest service
var postStream = request.GetRequestStream();
postStream.Write(bytes, 0, bytes.Length);
postStream.Close();
// Get the login status from the service
var response = (HttpWebResponse)request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var jsonResponseString = reader.ReadToEnd();
Following is the web.config
I am using:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="AviaryEndPointBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="AviaryServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" defaultOutgoingResponseFormat="Xml"
automaticFormatSelectionEnabled="true"></standardEndpoint>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule"
type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
<handlers>
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*"
path="UrlRouting.axd"
type="System.Web.HttpForbiddenHandler, System.Web, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</handlers>
</system.webServer>
</configuration>
However if I use the following Ajax Method I get nothing and there is no hit on the service:
postData.JobActionList = JSON.stringify(jobActionLists);
var postDataString = JSON.stringify(postData);
jQuery.ajax({
url: "http://myService.cloudapp.net/PerformAction",
type: "POST",
data: postDataString,
contentType: "application/json; charset=utf-8",
success: function (result) {
alert("Success" + result.d);
},
error: function (req, status, error) {
alert('Service Failed : ' + req + ", " + status + ", " + error);
}
});