有没有任何方法来捕获所有类型的控制台错误?实际上,我想将所有控制台错误存储到数据库中,这样我就可以修复PHP网站的严重问题。
下面是我当前捕获错误的代码,但是这段代码没有捕获内部服务器错误和其他类型的错误,比如
‘s:99未能在“DOMWindow”上执行“postMessage”:提供的目标源(“https://www.youtube.com”)与收件人窗口的来源不匹配
我现在的脚本
function ErrorSetting(msg, file_loc, line_no) {
var e_msg=msg;
var e_file=file_loc;
var e_line=line_no;
var error_d = "Error in file: " + file_loc +
"\nline number:" + line_no +
"\nMessage:" + msg;
if(logJsErrors){
theData = "file="+file_loc+"&line="+line_no+"&err="+msg;
ajaxCtrl(
function(){
return true;
},Helpers.RootURL()+"/logs/index",theData
);
}
if(isDebugging){
console.error(error_d);
}
return true;
}
window.onerror = ErrorSetting;我真的很感激你的努力。谢谢您:)
发布于 2018-06-19 12:34:51
您可以执行一种console.log原型,当您将console.log称为ajax激发时,您可以这样做:
(function () {
var postCons = console.log;
console.log = function (err) {
var xhttp = new XMLHttpRequest();
postCons.apply(this, arguments);
xhttp.open("POST", "log.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {//you could avoid this step
alert(this.responseText);//response from php
}
};
xhttp.send("data="+encodeURI(err));
}
})()在log.php中,您可以使用$_POST['data']做任何您想做的事情,您可以使用类似于urldecode($_POST['data'])的东西。
https://stackoverflow.com/questions/50927960
复制相似问题