Javascript 无法通过iframe添加cookie
我正在动态创建一个带有表单的iframe,并按如下方式提交此表单:Javascript 无法通过iframe添加cookie,javascript,c#,iframe,cookies,asp.net-web-api,Javascript,C#,Iframe,Cookies,Asp.net Web Api,我正在动态创建一个带有表单的iframe,并按如下方式提交此表单: var iframe = $("<iframe id='download_iframe' style='display: none' src='about:blank'></iframe>"); //...building that form... form.appendTo( iframe.contents().find('body') ).submit(); HttpResponseMessage
var iframe = $("<iframe id='download_iframe' style='display: none' src='about:blank'></iframe>");
//...building that form...
form.appendTo( iframe.contents().find('body') ).submit();
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
//...setting content type, disposition and etc...
response.Headers.AddCookies(new[]
{
new CookieHeaderValue("ajaxFileDownload", "true")
{
Expires = DateTimeOffset.Now.AddDays(1),
Domain = Request.RequestUri.Host,
Path = "/",
HttpOnly = false
}
});
因此,我希望在文件downlod启动后设置cookie,我可以在标题中看到set cookie
:
Cache-Control:private
Content-Disposition:attachment; filename=engagement_list_28-10-2014.pdf
Content-Length:60027
Content-Type:application/pdf
Date:Tue, 28 Oct 2014 16:12:09 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/7.5
Set-Cookie:ajaxFileDownload=true; expires=Wed, 29 Oct 2014 16:59:35 GMT; domain=localhost; path=/
X-AspNet-Version:4.0.30319
X-MiniProfiler-Ids:["a8e9bef2-d31e-4f41-8896-9222d880544f"]
X-Powered-By:ASP.NET
X-Powered-By:ARR/2.5
X-Powered-By:ASP.NET
但是
document.cookie
和$(“#download_iframe”).contents()[0]。cookie
是空的,我甚至在浏览器的资源中看不到该cookie。我做错了什么?这可能是由创建iframe的页面从本地主机以外的主机加载造成的。正在为localhost创建cookie,如果您创建的HTML是从文件系统或其他主机加载的,您将无法看到cookie
其他原因可能是
1) 浏览器的cookie策略
2) 运行浏览器的操作系统的时间。是否大于Expires字段?该页面是否会导致跨域问题?我不知道那页是怎么写的。