Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法不允许(http错误405)、wcf、rest服务、post方法_C#_Ajax_Rest_Wcf_Post - Fatal编程技术网

C# 方法不允许(http错误405)、wcf、rest服务、post方法

C# 方法不允许(http错误405)、wcf、rest服务、post方法,c#,ajax,rest,wcf,post,C#,Ajax,Rest,Wcf,Post,我试图从html、ajax脚本调用在wcf中实现的post方法。 我可以从邮递员那里调用。我试过谷歌,回答了前面的问题,但没有一个对我有帮助 wcf中的My web.config: <standardEndpoints> <webScriptEndpoint> <standardEndpoint name="" crossDomainScriptAccessEnabled="true" /> </webScri

我试图从html、ajax脚本调用在wcf中实现的post方法。 我可以从邮递员那里调用。我试过谷歌,回答了前面的问题,但没有一个对我有帮助

wcf中的My 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控制台的图像

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

但是我的wcf服务没有global.asax文件。很抱歉这个愚蠢的问题,因为我是c#新手。添加新文件(global.asax)还好吗?是的,应该可以。如果您有任何问题,请检查此项
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();
    }
}