背景: – 我创建了一个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
请帮我解决很多文章和博客,但仍然无法解决问题.
您的帮助将不胜感激!
专家请帮助我!