我为毕业设计了一个网站,但这仍然只是我要做的一件事。我想要的是,当脚本安装在网站上时,我想发送安装了我的脚本的网站的名称,而且每当出现错误时,我都想将其发送到我的网站,例如:
这个网站安装了我的脚本
www.security-dz.com/myscript
我想在其他网站的其他文件中查看路径+网站。例如:
www.getlog.com/mylogs.php
这样做的目的是让我的客户保持更新并为他们提供支持并查看发生的错误,以便我可以在下一次更新中修复它们。
最佳答案
您可能想仔细看看 JQuery docs for ajax requests ,因此您可以使用安全的 http 连接进行日志记录。此 javascript 代码基本上描述了一个将文本格式的错误发送到服务器端脚本的函数。该脚本可以依次将错误描述写入服务器上的文件。我建议改用数据库;这样您就可以轻松编写一个网络客户端来显示所有报告的错误(以及过滤器和其他好东西)。
您可以从服务器上的 ajax http get-request 中的 referer [sic] 字段中提取原始 url。
(function () { // function operator, in case console doesn't exist
!console ?
(console = {}) : console;
!console.log ?
(console.log = function () { }) : console.log;
!console.info ?
(console.info = console.log) : console.info;
!console.error ?
(console.error = console.log) : console.error;
}());
// Uses JQuery
function reportError (errDesc) {
var path = "www.getlog.com/mylogs.php";
$.ajax({
url: path,
type: "GET",
async: true,
cache: false,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
crossDomain: true,
data: errDesc,
dataType: "jsonp",
error: function (req, type, errObj) {
console.error("Reporting error failed: " + type + "\nAt url: " + path + "\n" + errObj);
// In case you need to debug the error reporting function
},
succes: function (res) {
console.info("Reported error to server:\nRequest:" + errDesc + "\nResponse: " + res);
// extra error logging facility on client-side, invisible to most users
},
global: false // prevent triggering global ajax event handlers
});
return errDesc; // in case you want to reuse the errDesc
}
代码已通过 jshint 验证。如果还有问题请告诉我,因为我没有花时间完全复制您的设置(设置 2 个不同的域等)
附录:如果您遇到跨域消息传递问题,请阅读一些有用的读物,JSON is not a subset of javascript , Cross-origin resource sharing , JSONP .
关于javascript - 将日志错误发送到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22730210/