【发布时间】:2016-11-21 22:45:48
【问题描述】:
背景:- 我创建了一个托管在本地系统 IIS 中的 WCF 服务。服务公开 GET/POST 方法并启用跨域,也可以使用 https 访问。为了使其可访问,使用了自签名证书。
测试:- 当我尝试执行跨域 ajax 调用时,它在 IE10/Edge 中适用于 GET 请求和 POST 请求(仅那些不接受数据作为 json 的 post 方法)。我可以在 chrome/Firebox 浏览器中对任何 GET/POST 请求进行跨域调用。只有在 ajax 调用中传递 contenttype:accept/json 参数时,IE 10/Edge 会导致跨域调用 POST 请求时出现问题。
研究:- 我阅读了很多博客/mdn 并了解了 IE 不忠实遵循 cors 的 cors 规范。我知道 cors 规范不会分配自定义标头/标头的值,因为 cors 预检中止了。
我正在制作的 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, ResponseFormat = WebMessageFormat.Json, UriTemplate = "PostUrlData/{value}")]
string PostUrlData(string value);
我已经在 WCF 的 Global.asax 的 BeginRequest 事件中编写了代码来处理 Options 请求:-
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 中启用Allow cross domain call 设置,因为最终用户不会执行此类步骤。
陷入问题:-但仍然无法在 IE 10/Edge(已启用 cors)中执行 JSON 数据发布调用。
(已编辑)更新: 托管 WCF 的 IIS 站点仅启用了匿名身份验证,而禁用了其他身份验证。 即使我尝试使用有效的 https 证书,但它仍然不适用于 IE,但适用于 chrome。
请求标头
OPTIONS https://service.domian.com/projectservice.svc/GetMultiListData HTTP/1.1
Accept: */*
Origin: https://sitename.servicedomain.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, accept
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: sitename.servicedomain.com
Content-Length: 0
Connection: Keep-Alive
Cache-Control: no-cache
响应标头
HTTP/1.1 200 OK
Cache-Control: no-cache,no-store
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: sitename.servicedomain.com
Access-Control-Allow-Methods: GET,POST,PUT,HEAD
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin,Content-Type,Accept,X-Requested-With,Session
Access-Control-Expose-Headers: DAV,content-length,Allow
Access-Control-Max-Age: 1728000
X-Powered-By: ASP.NET
Date: Thu, 04 Aug 2016 17:26:27 GMT
Content-Length: 0
请帮助我,因为我浏览了很多文章和博客,但仍然无法解决问题。 非常感谢您的帮助!
请专家帮助我!
【问题讨论】:
-
将dataType更改为“jsonp”是否允许请求?
-
我试过了,它允许请求但是错误的请求错误。
-
您是否尝试过 api fetch 而不是 $.ajax ? Fetch 是新的标准。 todojs.com/…
标签: jquery ajax wcf cors iis-7.5