jquery – 发布JSON ajax请求,cors在IE10 / Edge中不起作用

背景: – 我创建了一个WCF服务托管在我的本地系统IIS.服务公开GET / POST方法和跨域启用,也可以使用https访问.为了使其可访问,使用自签名证书.

测试: – 当我尝试做跨域ajax调用它可以工作正常的GET请求和POST请求(只有那些不接受数据作为json的post方法)在IE10 / Edge.我可以在chrome / FireBox浏览器中进行任何GET / POST请求的跨域调用.当contenttype:accept / json参数在ajax调用中传递时,只有IE 10 / Edge会导致跨域调用POST请求的问题.

研究: – 我读了很多博客/ mdn,并了解到IE不遵循宗教信仰的cors的规范.我知道cors规范不会因为哪些cors prelight中止而归因于custom header / header的值.

ajax请求的示例我正在做: –

var postDT = { "postValue": "test" };
        debugger;
        $.support.cors = true;
        $.ajax({
            type: "POST",data: JSON.stringify(postDT),url: "http://ateet3371/Service1.svc/postdata",contentType: "application/json; charset=utf-8",dataType: "JSON",processData: true,success: function (data) {
                 alert(data);
            },error: function (jqXHR,textStatus,errorThrown) {
                var a = jqXHR;
                alert(jqXHR + '---' + textStatus + '---' + errorThrown);
            }
        });

如果我删除了contentType:“application / json; charset = utf-8”,那么它会抛出错误的请求错误,否则它抛出访问被拒绝的错误.

而WCF中的方法实现是:

[OperationContract]
    [WebInvoke(Method = "POST",ResponseFormat = Webmessageformat.Json,UriTemplate = "PostResponseData")]
    string PostResponseData(PostDataTest postDT);

而数据通信是:

[DataContract]
public class PostDataTest
{
    private string post_value;

    // Apply the DataMemberAttribute to the property.
    [DataMember]
    public string postValue
    {

        get { return post_value; }
        set { post_value = value; }
    }
}

如果我使用方法PostUrl数据,那么ajax调用成功执行,并且如果ContentType:“Application / json”头被从请求中删除,则返回正确的结果.

[OperationContract]
    [WebInvoke(Method = "POST",RequestFormat = Webmessageformat.Json,UriTemplate = "PostUrlData/{value}")]
    string PostUrlData(string value);

我已经在WCF的Global.asax中的BeginRequest事件中编写代码来处理选项请求:

HttpContext.Current.response.addheader("Access-Control-Allow-Origin","*");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS" )
        { 
        //These headers are handling the "pre-flight" OPTIONS call sent by the browser
        HttpContext.Current.response.addheader("Access-Control-Allow-Methods","GET,POST,PUT,HEAD");
        HttpContext.Current.response.addheader("Access-Control-Allow-Credentials","true");
        HttpContext.Current.response.addheader("Access-Control-Allow-Headers","Origin,Content-Type,Accept,X-Requested-With,Session");
        HttpContext.Current.response.addheader("Access-Control-Expose-Headers","DAV,content-length,Allow" );
        HttpContext.Current.response.addheader("Access-Control-Max-Age","1728000" );
        HttpContext.Current.response.addheader("Cache-Control","no-cache,no-store");
        HttpContext.Current.Response.End();
        }

而且我无法在IE中启用允许跨域呼叫设置,因为最终用户不会执行此类步骤.

困扰问题: – 但是仍然无法在IE 10 / Edge中启用JSON数据(这是启用cors).

(已编辑)更新:
托管WCF的IIS站点仅启用匿名认证,而禁用其他身份验证.
即使我尝试使用https的有效证书,但仍然不适用于IE,但完美的Chrome.

请求头

选项https://service.domian.com/projectservice.svc/GetMultiListData HTTP / 1.1
接受:* / *
原产地:https://sitename.servicedomain.com
访问控制请求方法:POST
访问控制请求头:内容类型,接受
Accept-Encoding:gzip,deflate
User-Agent:Mozilla / 5.0(Windows NT 6.1; WOW64; Trident / 7.0; rv:11.0),如Gecko
主机:sitename.servicedomain.com
内容长度:0
连接:保持活力
缓存控制:无缓存

回应标题

HTTP / 1.1 200 OK
缓存控制:无缓存,无存储
服务器:Microsoft-IIS / 7.5
Access-Control-Allow-Origin:sitename.servicedomain.com
访问控制允许方法:GET,HEAD
Access-Control-Allow-Credentials:true
访问控制 – 允许标题:原始,内容类型,接受,X请求与会话
Access-Control-Expose-Headers:DAV,Allow
访问控制 – 最大年龄:1728000
X-Powered by:ASP.NET
日期:星期四,2016年8月04日17:26:27 GMT
内容长度:0

请帮我解决很多文章博客,但仍然无法解决问题.
您的帮助将不胜感激!

专家请帮助我!

解决方法

客户端可以检查几件事情:

你试图覆盖$.support.cors.这是(或者是)意味着是一个可读的属性,告诉你如果浏览器支持CORS. (见jQuery source查看哪里)
>基于jQuery docs for ajax,您可能需要将xhrFields:{withCredentials:true}添加到$.ajax()选项中
>使用dataType的正确外壳:“json”和charset = UTF-8

在服务器端,您可能希望在Access-Control-Allow-Origin响应头中尝试回应特定的主机名(回显Origin标头)而不是通配符(*).这在MDN中具体提到了一个还讨论Access-Control-Allow-Credentials的段落:

Important note: when responding to a credentialed request,server must specify a domain,and cannot use wild carding. The above example would fail if the header was wildcarded as: Access-Control-Allow-Origin: *.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...