问题描述
我正在运行一个 ASP.NET 站点,在那里我遇到了问题,仅通过手动测试就无法找到一些 JavaScript 错误.
Im running a ASP.NET Site where I have problems to find some JavaScript Errors just with manual testing.
是否有可能在客户端捕获所有 JavaScript 错误并将它们记录在服务器上,即在 EventLog 中(通过 Web 服务或类似的东西)?
Is there a possibility to catch all JavaScript Errors on the Clientside and log them on the Server i.e. in the EventLog (via Webservice or something like that)?
推荐答案
您可以尝试为 onerror 事件 并使用 XMLHttpRequest 告诉服务器出了什么问题,但是由于它不是任何规范的一部分,支持有点不稳定.
You could try setting up your own handler for the onerror event and use XMLHttpRequest to tell the server what went wrong, however since it's not part of any specification, support is somewhat flaky.
以下是使用 XMLHttpRequest 记录 JavaScript 错误的示例:
window.onerror = function(msg, url, line)
{
var req = new XMLHttpRequest();
var params = "msg=" + encodeURIComponent(msg) + '&url=' + encodeURIComponent(url) + "&line=" + line;
req.open("POST", "/scripts/logerror.php");
req.send(params);
};
这篇关于在服务器上记录客户端 JavaScript 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!