I have created a WCF rest service that is to be consumed by jquery. The service is hosted in my local machine. The service seems to work only if I am using a GET verb. If i use any other i get the 405 (Method not allowed ) error. Id like to post a json object to my service using jquery.
[ServiceContract]
public interface ITest
{
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate="Contact", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[Description("Send contact us message.")]
string Log(Someobject object);
}
CLient Code:
$.ajax({
type: "PUT",
url: "http://localhost/servicename/ContactUs.svc/Contact",
data: JSON.Stringify(jsonObject),
contentType: "application/json; charset=utf-8",
processData:true,
dataType: "jsonp",
success: function (data) {
alert("Ping" + data);
},
error: function (error) {
alert("Failed to ping server" + error);
}
});
Web Config
<behavior name="ContactUsBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ContactUsEndPointBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<services>
<service name="servicename.ContactUs" behaviorConfiguration="ContactUsBehaviour">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="JSONPBinding" contract="servicename.IContactUs" behaviorConfiguration="ContactUsEndPointBehavior"/>
</service>
</services>