【问题标题】:405 Method not allowed error in WCF web service Post MethodWCF Web 服务发布方法中的 405 Method not allowed 错误
【发布时间】:2017-03-25 09:52:34
【问题描述】:

我正在使用 WCF Web 服务并在从浏览器调用 POST 方法时收到错误“405 Method not allowed”,但从 PostMan 可以正常工作。 这是我的 WCF POST 方法代码

    [OperationContract]
    [WebInvoke(UriTemplate = "pushData", 
               Method = "POST", 
               ResponseFormat = WebMessageFormat.Json,
               RequestFormat = WebMessageFormat.Json, 
               BodyStyle = WebMessageBodyStyle.Bare)]
    bool pushDataForReporting(PushDataWrapUp data);

这是我的 web.config

   <system.web>
      <webServices>
         <protocols>
            <add name="HttpSoap" />
            <add name="HttpPost" />
            <add name="HttpGet" />
            <add name="Documentation" />
            <add name="HttpPostLocalhost" />
         </protocols>
      </webServices>
      <compilation targetFramework="4.5" debug="true" />
      <httpRuntime targetFramework="4.5" />
   </system.web>
   <system.serviceModel>
      <standardEndpoints>
         <webScriptEndpoint>
            <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
         </webScriptEndpoint>
      </standardEndpoints>
      <services>
         <service name="WrapUpWebService.WrapUp" behaviorConfiguration="ServiceBehaviour">
            <endpoint address="" binding="webHttpBinding" contract="WrapUpWebService.IWrapUp" behaviorConfiguration="web" />
         </service>
      </services>
      <behaviors>
         <serviceBehaviors>
            <behavior name="ServiceBehaviour">
               <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
               <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
               <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
         </serviceBehaviors>
         <endpointBehaviors>
            <behavior name="web">
               <webHttp />
            </behavior>
         </endpointBehaviors>
      </behaviors>
      <!--<protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>-->
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      <bindings>
         <webHttpBinding>
            <binding name="jsonpWebHttpBinding" crossDomainScriptAccessEnabled="true" />
         </webHttpBinding>
      </bindings>
   </system.serviceModel>
   <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <httpProtocol>
         <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
            <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />
            <add name="Access-Control-Max-Age" value="1728000" />
         </customHeaders>
      </httpProtocol>
      <directoryBrowse enabled="true" />
   </system.webServer>

这是我的 Javascript 代码

var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost/WrapUpWebService/WrapUp.svc/pushData",
"method": "POST",
"headers": {
"content-type": "application/json;charset=utf-8",
"cache-control": "no-cache",
"postman-token": "946a48f2-ae43-4595-62b2-0d9856bd4187"
},
  "processData": false,
  "data": "{\"AgentId\": \"12345\",\"RouterCallKey\":    \"Testing\",\"RouterCallKeyDay\":\"223344\", \"SkillGroupSkillTargetId\":\"12356\",\"SkillGroup\":\"BB\",\"DNIS\" : \"111\",\"ANI\":\"123456\",\"SelectedWrapUpCode\" :\"Testing & testing\"}"
}

 $.ajax(settings).done(function (response) {
 console.log(response);
});

浏览器响应

邮递员回应

【问题讨论】:

    标签: jquery asp.net web-services wcf


    【解决方案1】:

    尝试以下链接,您将获得问题的解决方案

    jQuery .ajax() POST Request throws 405 (Method Not Allowed) on RESTful WCF

    【讨论】:

    • 亲爱的 Vinoth,感谢您的回答。我已经检查过了,它已经在 IIS 和 web.config 中被允许,但它对我不起作用
    猜你喜欢
    • 1970-01-01
    • 2016-04-12
    • 2016-09-07
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多