在我的asp.net mvc网站上,当用户在某个文本框中粘贴某些内容时,我正在对我的服务器进行ajax调用。此调用过去在IE 8中工作,但现在它在IE 11中停止工作,在h.open(c.type,c.url,c.async)
的jQuery 1.7.1中给我一个访问被拒绝的异常。
长期研究暗示我可能与CORS问题有关,但......每次通话都在同一个域上。
<input type="text" id="Pasteexcelhere" name="Pasteexcelhere" />
<script type='text/javascript' >
$(function () {
onp();
});
function onp() {
obj = document.getElementById('Pasteexcelhere');
$('#Pasteexcelhere').on('paste', function (e) {
x = obj.value;
$.ajax({
type: "POST",
url: "<%= Url.Action("PasteFromExcel", "RequestData" ) %>",
data: "{'data': '" + x + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
traditional: true,
success: function (da) {
alert("success");
},
error: function (d) {
alert(d.statusText); // access denied
}
});
});
</script>
当尝试直接拨打同一个电话时,请通过一个简单的链接说明:
<a id="mylink" href="#" onclick="blubb();return false;">Pasted</a>
<script type='text/javascript' >
function blubb() {
obj = document.getElementById('Pasteexcelhere');
x = obj.value;
$.ajax({
type: "POST",
url: "<%= Url.Action("PasteFromExcel", "RequestData" ) %>",
data: "{'data': '" + x + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
traditional: true,
success: function (da) {
var propertyGrid = $('#RequestedTickers').data('tGrid');
propertyGrid.rebind({});
},
error: function (d) {
alert(d.statusText);
}
});
};
</script>
它按预期工作......(没有拒绝访问)
有人知道如何让它运行吗?
谢谢!
答案 0 :(得分:6)
由于它仅在粘贴时不起作用,因此问题似乎与粘贴事件处理程序有关。
在搜索IE11的问题和我发现的粘贴事件之后&#34; IE11 pasting clipboard data to an input element annoyance&#34;在StackOverflow上。
这可能是一个长镜头,但你可以尝试相同的解决方案&#34; (=解决方法)AlvinfromDiaspar在该帖子中提供answer:
$('#Pasteexcelhere').on('paste', function () { setTimeout(blubb, 100) });
答案 1 :(得分:1)
我有类似的问题。代码在 paste 事件监听器中触发了jQuery AJAX请求。这导致xhr.open(...)从下面显示的jQuery代码中抛出 Invalid Argument 异常。只有在 IE11 :
中运行代码时才会发生异常jQuery.ajaxTransport( function( options ) {
var callback, errorCallback;
// Cross domain only allowed if supported through XMLHttpRequest
if ( support.cors || xhrSupported && !options.crossDomain ) {
return {
send: function( headers, complete ) {
var i,
xhr = options.xhr();
xhr.open(
options.type,
options.url,
options.async,
options.username,
options.password
);
@lx建议的更改修复此问题。代码改为:
$('#my_input').on('paste', onPaste);
function onPaste(e)
{
triggerAjaxCall(...)
}
到
$('#my_input').on('paste', onPaste);
function onPaste(e)
{
setInterval(function () { triggerAjaxCall(...) }, 100);
}
答案 2 :(得分:0)
@lx感谢您,您的解决方案有效,但我的解决方案并没有使用JQuery,所以我不得不深入研究它。我没有得到拒绝访问权限因为@trykyn但是当我从<textarea onpaste="ThisIsMyPasteFunction()"></textarea>
我发现在我的情况下我必须使用onpaste="setTimeout(function () { ThisIsMyPasteFunction() }, 1)
,即使ThisIsMyPasteFunction
内部有setTimeout(function () { anotherfunction() } , 1)
,它也会一直作为空白的textarea值返回,直到我设置{ {1}}直接在setTimeout
。
希望如果他们不在代码中使用JQuery,这会有所帮助: - )
非常奇怪的是,右键单击和粘贴无法正常工作,但ctrl + v可以
此致 利安