Javascript 表单提交 - IE 访问被拒绝 - 同一个域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10667856/
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
form submit - IE access denied - same domain
提问by Lukasik
SCRIPT5: Access denied
jquery.min.js, line 3 char 3769
I'm getting this error by simple form submit only in IE
我只在 IE 中通过简单的表单提交收到此错误
$("#icon_upl").click(function(){ //icon_upl is button which open dialog
$("[name=icon]").click();
});
$("[name=icon]").change(function() { //icon is hidden file input
$("[name=upload_icon]").submit();
});
I sending that form to hidden iframe which is at the same domain.
我将该表单发送到位于同一域的隐藏 iframe。
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;display:none;"></iframe>
<form name="upload_icon" action="upload_icon.php" method="post" enctype="multipart/form-data" target="upload_target">
submit input doesn't help
提交输入没有帮助
I dont get it cuz if i try to send another form that works fine
我不明白,因为如果我尝试发送另一种工作正常的表格
回答by apoorvi
If you are triggering the select files dialog via JS then you will get an access denied error when submitting the form. IE does not allow this. You will have to ask user to click on input type file directly
如果您通过 JS 触发选择文件对话框,那么您将在提交表单时收到拒绝访问错误。IE 不允许这样做。您将不得不要求用户直接单击输入类型文件
More details here https://github.com/valums/file-uploader/issues/118#issuecomment-1387612
更多细节在这里 https://github.com/valums/file-uploader/issues/118#issuecomment-1387612
You can try styling the input type file though http://www.quirksmode.org/dom/inputfile.html
您可以尝试通过http://www.quirksmode.org/dom/inputfile.html设置输入类型文件的样式
回答by WynandB
I had similar HTML and jQuery code and encountered the same issue (i.e. 'Access is denied.' JavaScript error in Internet Explorer), which I managed to resolve by taking pointers from this(great) answer.
我有类似的 HTML 和 jQuery 代码并遇到了同样的问题(即“访问被拒绝。”Internet Explorer 中的 JavaScript 错误),我设法通过从这个(很棒的)答案中获取指针来解决这个问题。
In your instance:
在您的实例中:
Change the #icon_upl
<button>
/<input>
to a<label>
and make use of the tag's accessibility features by setting thefor
attribute on it to point to your<input name="icon" type="file">
element.
This effectively makes yourclick()
event handler redundant. However, clicking the<label>
in Firefox does not seem to trigger the file<input>
dialog so you'll need to perform a browser test and still have theclick()
event handler if the browser is Mozilla-based.In order for it to work, you'll need to make sure that your file
<input>
is not hidden by setting its position to be absolute and moving it off-screen.
将 #icon_upl
<button>
/更改<input>
为 a<label>
并通过将其for
上的属性设置为指向您的<input name="icon" type="file">
元素来利用标签的辅助功能。
这有效地使您的click()
事件处理程序变得多余。但是,<label>
在 Firefox 中单击似乎不会触发文件<input>
对话框,因此click()
如果浏览器是基于 Mozilla 的,您需要执行浏览器测试并且仍然具有事件处理程序。为了使其正常工作,您需要
<input>
通过将其位置设置为绝对位置并将其移出屏幕来确保您的文件未被隐藏。
回答by Benoit
i have found an other way to do this ... I have make test and i found it work after 2 or 3 click on the submit button.
我找到了另一种方法来做到这一点......我进行了测试,我发现它在点击提交按钮 2 或 3 次后可以工作。
i have try some solution but found this by my self. this is only for ie.
我尝试了一些解决方案,但我自己发现了这个。这仅适用于即。
note i dont use the jquery submit method because they handle the error.
请注意,我不使用 jquery submit 方法,因为它们会处理错误。
function Submit() {
try {
$('#FormName')[0].submit();
} catch (e) {
setTimeout(function () { Submit(); }, 50);
}
}
ps. sorry for my bad english, this is not my first language.
附:抱歉我的英语不好,这不是我的母语。
回答by thecodeparadox
You can make a direct event firing on hidden input field because you can't catch it. It is possible to bind event with it and trigger it via another.
您可以在隐藏的输入字段上触发直接事件,因为您无法捕捉到它。可以将事件与其绑定并通过另一个事件触发它。
for example:
例如:
// binding event to hidden field
$('input[name=icon]:hidden').on('click', function() {
alert('Hidden triggered');
});
// some button/ or else
// some_target is any valid selector you can use
$('some_target').on('click', function() {
$('input[name=icon]:hidden').click(); // triggering click on hidden field will alert 'Hidden triggered'
});
Note: But its not clear from your post that if you have already something like this or not.
注意:但是从您的帖子中不清楚您是否已经拥有这样的东西。
回答by Lukasik
It seems to be impossible
似乎不可能
- You cannot read the "value" of the element as it holds the filename.
- You can't fire up the file selection menu via JS.
- You can't fire submit of the file uploader control via JS.
- 您无法读取元素的“值”,因为它包含文件名。
- 您无法通过 JS 启动文件选择菜单。
- 您不能通过 JS 触发文件上传器控件的提交。