Ajax 在Firefox中获取WCF POST REST请求的CORS错误

Ajax 在Firefox中获取WCF POST REST请求的CORS错误,ajax,wcf,post,Ajax,Wcf,Post,我在Firefox中收到WCF POST请求的CORS错误。此代码在Edge和Chrome中运行良好。该错误仅在Firefox中出现。此外,只有当我在调用时将数据参数集和contentType设置为“Application/Json;charset=utf-8”,才会在Firefox中看到这个错误。如果将contentType更改为“text/plain”,则会出现“400错误请求”错误。我的代码如下: WCF中的选项堆: [WebInvoke(Method = "OPTIONS", UriTe

我在Firefox中收到WCF POST请求的CORS错误。此代码在Edge和Chrome中运行良好。该错误仅在Firefox中出现。此外,只有当我在调用时将数据参数集和contentType设置为“Application/Json;charset=utf-8”,才会在Firefox中看到这个错误。如果将contentType更改为“text/plain”,则会出现“400错误请求”错误。我的代码如下:

WCF中的选项堆:

[WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
    public void GetOptions()
    {
        if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "https://localhost:5001");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS,HEAD,PUT,DELETE");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Max-Age", "1728000");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "x-requested-with, Content-Type, origin, authorization, accept, client-security-token");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Credentials", "true");
        }
    }
我正在调用的post方法:

 [WebInvoke(UriTemplate = "GetAttachmentsPost1", RequestFormat = WebMessageFormat.Json,          ResponseFormat = WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.Bare, Method = "POST")]
    public void GetAttachmentsPost1( teststr teststr1)
    {

        string  str = "abc";

    }
[DataContract]
  public class teststr
{ 
[DataMember]
public string teststr1 { get; set; }
}

对服务的AJAX调用:

 $.ajax({

            url: "http://127.0.0.1:5555/MyWCFService/GetAttachmentsPost1",

            type: "POST",

            data: JSON.stringify({ 'teststr1':'abc'} ),

            xhrFields: {
                withCredentials: true
            },
            contentType: "application/json; charset=utf-8",


            crossDomain: true,
            dataType: "JSON",
            processData: true,
            error: function (data) {

            },
            success: function (data) {

            },
            statusCode: {
                404: function () {
                    //     page not found
                }
            },
            complete: function (data) {

            },
        });
正如我上面提到的,它在Edge和Chrome中工作良好,但在FF中仅失败。在Edge和Chrome中,对选项进行飞行前呼叫,但在FF中,呼叫不进行

感谢您的帮助


谢谢。

这个问题发生在Firefox上,因为我的Web应用程序是HTTPS,而我的WCF服务是HTTP。对于Firefox来说,这是一个CORS问题:


解决方案在于同时在https上运行WCF服务。

浏览器在devtools控制台中记录的确切错误消息是什么?在Firefox中:跨源请求被阻止:同源策略不允许读取远程资源。(原因:CORS请求未成功)响应的HTTP状态代码是什么?您可以使用浏览器devtools中的网络窗格进行检查。这是4xx还是5xx错误,而不是200 OK成功响应?Firefox在网络窗格中没有显示对此的任何调用。这在飞行前检查中似乎失败了。如果我没有传递-data:JSON.stringify({'teststr1':'abc'})参数,请删除contentType:“application/JSON;charset=utf-8”,然后调用传递并命中WCF方法。有趣的是,如果在Firefox中的POST请求中,我将数据作为查询字符串传递,如:127.0.0.1:5555/MyWCFService/GetAttachmentsPost1?data=data,那么它就可以工作。但是,如果我将数据作为参数数据传递:数据,就像我们在POST请求中所做的那样,它将失败!