簡體   English   中英

方法不允許(http錯誤405),wcf,休息服務,post方法

[英]Method not allowed (http error 405) , wcf , rest service , post method

我試圖從html,ajax腳本調用在wcf中實現的post方法。 我可以從郵遞員那里調用。 我試過谷歌,stackoverflow以前的問題,但沒有一個幫助我。

來自wcf的我的web.config:

  <standardEndpoints>
      <webScriptEndpoint>
        <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
      </webScriptEndpoint>
    </standardEndpoints>

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,OPTIONS" />
  </customHeaders>
</httpProtocol>

以下是我在html中的ajax調用

  $.ajax({
      type: "POST",
      url: "http://localhost:8078/SportsClubDefault.svc/Validate",
      data: st,
       contentType: "application/json; charset=utf-8",
       processData:true,
      dataType: "json",
      success: function (data) {
          // Play with response returned in JSON format
            alert('Login success');
      },
       error: function (xhr) {
             alert('Login failed');
         }
     });

以下是郵遞員的照片 在此輸入圖像描述

以下是html控制台的圖像 在此輸入圖像描述

這是一個CORS問題,您可以通過在wcf服務上創建global.asax文件來啟用cors,

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("Access-Control-Allow-Methods", "POST, PUT, DELETE");

        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM