【问题标题】:405 response error from .net WCF Service来自 .net WCF 服务的 405 响应错误
【发布时间】:2012-12-09 09:44:53
【问题描述】:

我正在尝试在 asp.net 中为我的应用程序编写后端,但在来自 Ext 的 Ajax 请求后,我的 WCF 服务无法返回 POST 响应。相反,我收到 405 'Method not allowed' 错误。

这是我的要求:

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);
    }
});

这是我的界面:

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

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

        #endregion
    }
}

和服务代码:

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();
    }
}

最后是配置:

<?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>

【问题讨论】:

  • 第一个猜测:我想说你需要去掉 ajax 请求中的“参数”,因为该方法不需要任何参数。

标签: c# asp.net wcf request http-status-code-405


【解决方案1】:

这可能是一个跨域问题。例如,如果您的服务位于 http://localhost:35798 并且发出 AJAX 请求的网页位于 http://localhost:80 ,则浏览器仍将其视为 不同 域(即使只有端口号不同)。在这种情况下,某些浏览器发出 CORS 请求,如果您的服务不处理它,将返回 405。您的选择是(1)避免跨域请求,(2)实施 CORS(不会帮助浏览器不支持它),或(3)使用 JSONP。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 2012-10-12
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    相关资源
    最近更新 更多