Sara
Sara

Reputation: 93

passing JSON object to WCF service using AJAX from clientside not working in firefox

I want to pass JSON object to WCF service using AJAX from clientside.Everything works fine in Internet Explorer,but not in firefox.

In Firefox i'm getting a 405:Method not allowed

This is where i'm passing json data (from Client script) to a WCF service...

    $(document).ready(function () {
        var Author = '{ "Id": "A01", "Name": "Ravinder" }';
        $.ajax({
            type: "POST",
            data: JSON.stringify(Author),
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: "http://localhost:53905/Service1.svc/AuthorPostByJson",
            success: function (data) {
                alert("success");
            },

            error: function (xmlhttprequest, textstatus, errorthrown) {
                alert(" failed ");
                console.log("error: " + errorthrown);
            }
         });//end of $.ajax
    });

My WCF service is like ...

     [OperationContract]
            [WebInvoke(Method = "POST",
                UriTemplate = "AuthorPostByJson", 
                ResponseFormat = WebMessageFormat.Json, 
                RequestFormat = WebMessageFormat.Json)]       
            List<Book> GetBooksByAuthor_JSON(Author author);

My web.config file ....

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="Platform.WebRestful.Service1Behavior"
        name="Platform.WebRestful.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="Platform.WebRestful.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
      <service behaviorConfiguration="Platform.WebRestful.BookServiceHostRestfulBehavior"
        name="Platform.WebRestful.BookServiceHostRestful">
        <endpoint address="" binding="webHttpBinding" contract="Platform.WebRestful.IBookServiceHostRestful">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Platform.WebRestful.Service1Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="Platform.WebRestful.BookServiceHostRestfulBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
   <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

Upvotes: 1

Views: 1278

Answers (1)

Sara
Sara

Reputation: 93

Finally i found answer in some article. They said across any Cross-Domain(site) HTTP requests, first browser will send ‘OPTIONS’ Request called as “Preflight Request”... “preflighted” requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send and this request expects appropriate headers saying that service is allowing to access the service as a Response

To achieve this we have two solutions... 1) WCF Custom behaviors 2) Modifying the Global.asax file’s Application_BeginRequest event.

I followed the second one... Solution is to add a Global.asax file to the WCf service project and add the

following code in that,then it perfectly works across any browser...

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("Cache-Control", "no-cache");
                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();
            }
        }

Upvotes: 1

Related Questions