Javascript 未捕获的错误:SECURITY_ERR:当我尝试设置 cookie 时出现 DOM 异常 18

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2704929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:33:04  来源:igfitidea点击:

Uncaught Error: SECURITY_ERR: DOM Exception 18 when I try to set a cookie

javascriptjqueryhtmldomexception

提问by Pieter

I get the following error in Chrome's developer tools window when I try to set a cookie using thisjQuery plugin:

当我尝试使用jQuery 插件设置 cookie 时,我在 Chrome 的开发人员工具窗口中收到以下错误:

Uncaught Error: SECURITY_ERR: DOM Exception 18

未捕获的错误:SECURITY_ERR:DOM 异常 18

What does this error mean and how can I fix it? I get the same error when I use thisjQuery plugin.

这个错误是什么意思,我该如何解决?当我使用这个jQuery 插件时,我得到了同样的错误。

回答by Eli Grey

You're most likely using this on a local file over the file://URI scheme, which cannot have cookies set. Put it on a local server so you can use http://localhost.

您最有可能通过file://URI 方案在本地文件上使用它,该方案不能设置 cookie。将其放在本地服务器上,以便您可以使用http://localhost.

回答by alocaly

I also had this issue while developping on HTML5 in local. I had issues with images and getImageData function. Finally, I discovered one can launch chrome with the --allow-file-access-from-file command switch, that get rid of this protection security. The only thing is that it makes your browser less safe, and you can't have one chrome instance with the flag on and another without the flag.

我在本地开发 HTML5 时也遇到了这个问题。我遇到了图像和 getImageData 函数的问题。最后,我发现可以使用 --allow-file-access-from-file 命令开关启动 chrome,从而摆脱这种保护安全性。唯一的问题是它使您的浏览器不那么安全,并且您不能让一个带有标志的 chrome 实例和另一个没有标志的实例。

回答by Ibolit

You can also "fix" this by replacing the image with its inline Base64 representation:

您还可以通过用其内联 Base64 表示替换图像来“修复”此问题:

img.src= "data:image/gif;base64,R0lGODlhCwALAIAAAAAA3pn/ZiH5BAEAAAEALAAAAAALAAsAAAIUhA+hkcuO4lmNVindo7qyrIXiGBYAOw==";
有用,当您不打算在网络上发布页面,而是仅在本地机器上使用它时。

回答by Endre Simo

Faced with the same situation playing with Javascript webworkers. Unfortunately Chrome doesn't allow to access javascript workers stored in a local file.

面对与 Javascript webworkers相同的情况。不幸的是,Chrome 不允许访问存储在本地文件中的 javascript 工作人员。

One kind of workaround below using a local storage is to running Chrome with --allow-file-access-from-files(with sat the end), but only one instance of Chrome is allowed, which is not too convenient for me. For this reason i'm using Chrome Canary, with file access allowed.

下面使用本地存储的一种解决方法是使用--allow-file-access-from-filess最后)运行 Chrome ,但只允许一个 Chrome 实例,这对我来说不太方便。出于这个原因,我使用 Chrome Canary,并允许文件访问。

BTW in Firefox there is no such an issue.

顺便说一句,Firefox 中没有这样的问题。

回答by atimb

This error pops up, if you try to create a web worker with data URI scheme.

如果您尝试使用数据 URI 方案创建 Web Worker,则会弹出此错误。

var w = new Worker('data:text/javascript;charset=utf-8,onmessage%20%3D%20function()%20%7B%20postMessage(%22pong%22)%3B%20%7D'); w.postMessage('ping');

It's not allowed according to the standard: http://www.whatwg.org/specs/web-apps/current-work/multipage/workers.html#dom-worker

根据标准不允许这样做:http: //www.whatwg.org/specs/web-apps/current-work/multipage/workers.html#dom-worker

回答by Mr_Pouet

I had this issue when using the history API.

我在使用历史 API 时遇到了这个问题。

window.history.pushState(null, null, URL);

Even with a local server (localhost), you want to add 'http://' to your URL so that you have something similar to:

即使使用本地服务器 (localhost),您也希望将“http://”添加到您的 URL 中,以便您拥有类似于以下内容的内容:

http://localhost...

回答by alocaly

I wasn't completely happy by the --allow-file-access-from-filessolution, because I'm using Chrome as my primary browser, and wasn't really happy with this breach I was opening.

我对--allow-file-access-from-files解决方案并不完全满意,因为我使用 Chrome 作为我的主要浏览器,并且对我打开的这个漏洞并不满意。

Now I'm using Canary ( the chrome beta version ) for my development with the flag on. And the mere Chrome version for my real blogging : the two browser don't share the flag !

现在我正在使用 Canary(chrome beta 版)进行我的开发,并带有标志。我真正的博客只有 Chrome 版本:这两个浏览器不共享标志!

回答by Thomas Hunter II

One can also receive this error if using the new (so far webkit only) notification feature before getting permission.

如果在获得许可之前使用新的(目前仅限 webkit)通知功能,也可能会收到此错误。

First run:

第一次运行:

<!-- Get permission -->
<button onclick="webkitNotifications.requestPermission();">Enable Notifications</button>

Later run:

稍后运行:

// Display Notification:
window.webkitNotifications.createNotification('image', 'Title', 'Body').show();

The request permission functions needs to be triggered from an event caused by the user, otherwise it won't be displayed.

请求权限功能需要由用户引起的事件触发,否则不会显示。

回答by anewcomer

I was been getting that error in mobile safari when using ASP.NET MVC to return a FileResult with the overload that returns a file with a different file name than the original. So,

在使用 ASP.NET MVC 返回 FileResult 时,我在移动 safari 中遇到了该错误,该重载返回文件名与原始文件名不同的文件。所以,

return File(returnFilePath, contentType, fileName);

would give the error in mobile safari, where as

会在移动 safari 中给出错误,其中

return File(returnFilePath, contentType);

would not.

不会。

I don't even remember why I thought what I was doing was a good idea. Trying to be clever I guess.

我什至不记得为什么我认为我在做的事情是个好主意。我猜想变得聪明。