В IE, когда я вызываю метод страницы из события OnPaste, я получаю следующую ошибку, когда я нажимаю на поле ввода и нажимаю CTRL + V:
0x80070057 - JavaScript runtime error: Invalid argument.
на этой строке кода:
this._xmlHttpRequest.open(verb, this._webRequest.getResolvedUrl(), true );
Стек вызова выглядит следующим образом:
Sys$Net$XMLHttpExecutor$executeRequest [ScriptResource.axd] Line 6055 Script
Sys$Net$_WebRequestManager$executeRequest [ScriptResource.axd] Line 6302 Script
Sys$Net$WebRequest$invoke [ScriptResource.axd] Line 6481 Script
Sys$Net$WebServiceProxy$invoke [ScriptResource.axd] Line 6923 Script
Sys$Net$WebServiceProxy$_invoke [ScriptResource.axd] Line 6807 Script
PageMethods.prototype.Message [WebForm1.aspx] Line 76 Script
PageMethods.Message [WebForm1.aspx] Line 117 Script
GetMessage [WebForm1.aspx] Line 11 Script
onpaste [WebForm1.aspx] Line 125 Script
Я воспроизвел его в небольшом проекте. Здесь WebForm1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PageMethodTest.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type='text/javascript'>
function GetMessage() {
PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
}
function OnGetMessageSuccess(result, userContext, methodName) {
alert(result);
}
function OnGetMessageFailure(error, userContext, methodName) {
alert(error.get_message());
}
</script>
</head>
<body>
<form id='form1' runat='server'>
<input value="paste here" onpaste="GetMessage()" onblur="GetMessage()" />
<asp:ScriptManager ID='ScriptManager1' runat='server' EnablePageMethods='true' />
<div>
<input type='submit' value='Get Message' onclick='GetMessage(); return false;' />
</div>
</form>
</body>
</html>
Здесь WebForm1.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PageMethodTest
{
public partial class WebForm1 : System.Web.UI.Page
{
[System.Web.Services.WebMethod]
public static string Message()
{
return "Hello from the server-side World!";
}
}
}
Я вызвал тот же метод страницы из события OnBlur, и когда вы выходите из поля ввода, он работает нормально. Это событие OnPaste, вызывающее PageMethod, отлично работает в Chrome. Есть ли какая-то разница в контексте события OnPaste для IE и большинства других событий?
Благодарю.
Не уверен точно, что происходит, но симптомы такие же, как проблема, описанная в этом вопросе. Некоторая проблема с IE в сочетании с событием onpaste
сочетании с созданием запроса AJAX.
Я смог избавиться от проблемы, используя setTimeout
с небольшой задержкой:
function GetMessage() {
setTimeout(function () {
PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
}, 100);
}
setTimeout
как указано в этом вопросе, проблема исчезнет.