Вопрос

I'm trying to write a backend for my app in asp.net but I can't get my WCF service return a POST response after Ajax request from Ext. Instead I'm getting a 405 'Method not allowed' error.

This is my request :

Ext.Ajax.request({
    type: 'POST',
    url: 'http://localhost:35798/RestServiceImpl.svc/export',
    params: {
        html        : {'array': document.body.innerHTML}
    },
    success : function(response){
    console.log('RESPONSE !', response);
        //me.onSuccess(response, callback, errback);
    },
    failure : function(response){
        console.log('RESPONSE FAIL !', response);
    }
});

This is my interface :

namespace RestService
{
    public class RestServiceImpl : IRestServiceImpl
    {
        #region IRestServiceImpl Members

        public string JSONData()
        {
            return "Your POST request";
        }

        #endregion
    }
}

and the service code :

using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web.Script.Services;

namespace RestService
{

    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [ScriptMethod]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "export")]
        string JSONData();
    }
}

And finally the config :

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="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>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>
Это было полезно?

Решение

This may be a cross-domain issue. E.g if your service is at http://localhost:35798 and the web page making the AJAX request is at http://localhost:80 , it's still considered a different domain by the browser (even though only the port number is different). Some browsers in this case issue a CORS request, and if your service doesn't handle it, will return a 405. Your options then are to either (1)avoid cross-domain requests, (2)implement CORS (will not help browsers that don't support it), or (3) use JSONP.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top