0

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>
CodeNoob
  • 411
  • 1
  • 7
  • 24

1 Answers1

0

See dataType: "jsonp" only work with type:"GET". so if you are using jsonp then you have to use this:

type: "GET", // or remove it, if not available then "GET" is default set
.......
dataType: "jsonp",

Another answer which can help you.


After your comment i suggest you to do this:

$.ajax({
     type: "GET", // change to GET
     url: "/servicename/ContactUs.svc/Contact", // remove the http://localhost
     data: JSON.Stringify(jsonObject),
     contentType: "application/json; charset=utf-8",
     processData:true,
     dataType: "json",   // change jsonp to json
     success: function (data) {
         alert("Ping" + data);
     },
     error: function (error) {
         alert("Failed to ping server" + error);
     }
});

Or if you still want to use http://localhost then you have to enable cors at your serverside.

A post about enabling CORS (Cross Origin Resource Sharing.)

Community
  • 1
  • 1
Jai
  • 74,255
  • 12
  • 74
  • 103
  • My aim is to send an object to the server as a parameter hence im using a type:put or post. If i remove jsonp and use type post or put I get the following: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.'I also get (options {my service url} 405 method not allowed – CodeNoob May 28 '14 at 13:39